OrderedDict([('red', 10.0),('blue',45.5),('green',34.4)]
xList[(u'blue',1.5,u'car'),(u'green',1.9,u'bike'),(u'red', 10,u'boat')]
如何通过将值xList[1]
多重化来遍历orderedDict和List来更新ordedDict值?
希望有意义的是我在过去的几个晚上都在努力解决这个问题,我们将非常感谢任何帮助。
理想情况下,我想回来:
OrderedDict[('red',100),('blue',68.25),('green',65.36)]
答案 0 :(得分:1)
from collections import OrderedDict
d = OrderedDict([('red', 10.0),('blue',45.5),('green',34.4)])
xList = [(u'blue',1.5,u'car'),(u'green',1.9,u'bike'),(u'red', 10,u'boat')]
for tup in xList: # iterate over each tuple
if tup[0] in d: # make sure key is in the dict
d[tup[0]] *= tup[1] # multiply current value by the second element of the tuple
print(d)
OrderedDict([('red', 100.0), ('blue', 68.25), ('green', 65.36)])