【发布时间】:2019-04-17 14:52:43
【问题描述】:
我有一个元组列表,如下所示:
lst_of_tpls = [(1, 'test2', 3, 4),(11, 'test12', 13, 14),(21, 'test22', 23,24)]
我想把它转换成字典,让它看起来像这样:
mykeys = ['ones', 'text', 'threes', 'fours']
mydict = {'ones': [1,11,21], 'text':['test2','test12','test22'],
'threes': [3,13,23], 'fours':[4,14,24]}
我曾尝试像这样枚举lst_of_tpls:
mydict = dict.fromkeys(mykeys, [])
for count, (ones, text, threes, fours) in enumerate(lst_of_tpls):
mydict['ones'].append(ones)
但这会将我希望在“ones”中看到的值也放在其他“类别”中:
{'ones': [1, 11, 21], 'text': [1, 11, 21], 'threes': [1, 11, 21], 'fours': [1, 11, 21]}
另外,我想让mykeys 保持灵活。
【问题讨论】:
-
@BearBrown 你不必同意吗?不知道你为什么要告诉我这个?
标签: python