python tuple to dict

前端 未结 6 684
無奈伤痛
無奈伤痛 2020-11-27 10:17

For the tuple, t = ((1, \'a\'),(2, \'b\')) dict(t) returns {1: \'a\', 2: \'b\'}

Is there a good way to get {\'a\': 1, \'

6条回答
  •  误落风尘
    2020-11-27 10:54

    A slightly simpler method:

    >>> t = ((1, 'a'),(2, 'b'))
    >>> dict(map(reversed, t))
    {'a': 1, 'b': 2}
    

提交回复
热议问题