from collections import OrderedDict
l = [('Monkey', 71), ('Monkey', 78), ('Ostrich', 80), ('Ostrich', 96), ('Ant', 98)]
d = OrderedDict()
for i, j in l:
d[i] = j
print d
OrderedDict([('Monkey', 78), ('Ostrich', 96), ('Ant', 98)])
预期“d”应该是:把物品放入字典不改变为了
OrderedDict([('Monkey', (71,78)), ('Ostrich', (80,96)), ('Ant', 98)])
如果所有值都tupled或上市没有问题。
为什么这是预期的输出?每个键只能有一个值,此时您将*替换上一个值。 – jonrsharpe
是的预期输出应该包括所有相应的值,但不知道如何去做。 – jean
你需要仔细检查你的代码,并理解'd [i] = j'实际上在你的代码中做了什么。看看你期待的数据结构。看看你在做什么。确定如何在更新字典中的值的上下文中正确创建该数据结构。使用文档卢克。 – idjaw