gpt4 book ai didi

python - 将一个 python 字典中的键映射到另一个字典的确切值

转载 作者:行者123 更新时间:2023-11-28 16:28:49 24 4
gpt4 key购买 nike

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]

最佳答案

使用 dictionary comprehension :

>>> 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'}

关于python - 将一个 python 字典中的键映射到另一个字典的确切值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34173487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com