letters = ['a', 'b', 'c', 'd', 'e', 'f']
numbers = ['judge1','judge2','judge3','judge4','judge5']
dictionary = {}
for letter in letters:
for number in numbers:
roundDictionary[letter] = {number : None}
我希望字典是:
dictionary = {'a' : {'1' : None, '2' : None, '3' : None, '4' : None, '5' :
None}}
以此类推,但字典仅显示列表最后一个位置的项,如下所示:
{'a': {'5': None}, 'b': {'5': None}, 'c': {'5': None}, 'd': {'5': None}, 'e': {'5': None}, 'f': {'5': None}}
我怎样才能把所有的数字都加到字典里呢?谢谢
问题是您正在将值重新分配给每个循环中的键。实际上,每次都会覆盖它,这就是为什么只看到最后一个值。试试这个
编辑:来自@Joran Beasley的评论是在发布这篇文章时出现的。他的方法比使用嵌套循环和检查键是否存在要干净得多
相关问题 更多 >
编程相关推荐