2014-11-13 3 views
1
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')] 

어떻게 orderedDict을 통해 내가 루프를하고 목록 xList[1]에 의해 값을 mulitplying하여 ordedDict 값을 업데이트?업데이트 orderedDIct 값 [1]

내가 지난 몇 밤 동안 이것과 고투하고있는 희망과 희망은 크게 인정 될 것이다.

이상적으로 내가 다시 싶어`xList`은 무엇

OrderedDict[('red',100),('blue',68.25),('green',65.36)] 
+0

입니까? –

+0

내 이름을 지은 것뿐입니다. – JayMc

답변

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)])