Python 如何将元组转换为字典,然后附加到列表

Python 如何将元组转换为字典,然后附加到列表,python,Python,我想做的是编一本字典,然后把它附加到一个主列表中 前 我想要的是: [{1:3, 4:6},{4:3,2:4}] 我的代码 masterList2 = [] for tuples in x: myDict = {} for singleTuple in tuples: myDict[singleTuple[0]] = singleTuple[1] masterList2.append(myDict) print masterList2 您可以使用和:

我想做的是编一本字典,然后把它附加到一个主列表中

我想要的是:

[{1:3, 4:6},{4:3,2:4}]
我的代码

masterList2 = []
for tuples in x:
    myDict = {}
    for singleTuple in tuples:
        myDict[singleTuple[0]] = singleTuple[1]
    masterList2.append(myDict)
print masterList2
您可以使用和:

或者,根据您的喜好,您可以使用和
dict

>>> x = [[(1,3),(4,6)],[(4,3),(2,4)]]
>>> map(dict, x)
[{1: 3, 4: 6}, {2: 4, 4: 3}]
>>>
不过,大多数Python程序员更喜欢列表理解。

您可以使用and:

[{key:value for key,value in i} for i in x]
或者,根据您的喜好,您可以使用和
dict

>>> x = [[(1,3),(4,6)],[(4,3),(2,4)]]
>>> map(dict, x)
[{1: 3, 4: 6}, {2: 4, 4: 3}]
>>>
不过,大多数Python程序员更喜欢列表理解

[{key:value for key,value in i} for i in x]