现在我有一个单词和数字列表:
List = [("purple", 8), ("orange", 2),...]
我想把它变成字典:
Dictionary = [{"word":" purple", "number":8}, ...]
我该如何解决这个问题?在此先感谢您的任何帮助
答案 0 :(得分:4)
In [3]: [{'word': x, 'number': y} for x, y in List]
Out[3]: [{'word': 'purple', 'number': 8}, {'word': 'orange', 'number': 2}]
虽然你很可能想要这个:
In [4]: dict(List)
Out[4]: {'orange': 2, 'purple': 8}
答案 1 :(得分:1)
Dictionary = [{"word": word, "number": number} for word, number in List]