mydict={'f1':'stream','f2':'status','f3':'remarks'}
mydict1={'stream':'RHEL','status':'pass','remarks':'none'}
必需的输出
result={'f1':'RHEL','f2':'pass','f3': 'none'}
def function(mydict, mydict1):
for key,value in mydict1.iteritems():
print mydict1[key]
答案 0 :(得分:2)
>>> mydict = {'f1': 'stream', 'f2': 'status', 'f3': 'remarks'}
>>> mydict1 = {'stream': 'RHEL', 'status': 'pass', 'remarks': 'none'}
>>> {key: mydict1[value] for key, value in mydict.items()}
{'f1': 'RHEL', 'f2': 'pass', 'f3': 'none'}
答案 1 :(得分:1)
要修复代码,请改为遍历mydict.items()
,然后以这种方式附加到result
字典:
>>> mydict={'f1':'stream','f2':'status','f3':'remarks'}
>>> mydict1={'stream':'RHEL','status':'pass','remarks':'none'}
>>>def function(mydict, mydict1):
for key,value in mydict.items():
result[key] = mydict1[value]
return result
>>>function(mydict, mydict1)
{'f1': 'RHEL', 'f2': 'pass', 'f3': 'none'}
当然,使用falsetru在答案中张贴的dictionary comprehension可以大大简化。