Python字典:获取键列表的值列表 [英] Python dictionary: Get list of values for list of keys
本文介绍了Python字典:获取键列表的值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一个内置的/快速的方式来使用字典列表来获取相应的项目列表?例如我有: / p>
>>> mydict = {'one':1,'two':2,'three':3}
>>> mykeys = ['three','one']
如何使用 mykeys
以字典形式列出相应的值?
>>> mydict.WHAT_GOES_HERE(mykeys)
[3,1]
解决方案>
列表理解似乎是一个很好的方法:
>>> [mydict [x] for my in mykeys]
[3,1]
Is there a built-in/quick way to use a list of keys to a dictionary to get a list of corresponding items?
For instance I have:
>>> mydict = {'one': 1, 'two': 2, 'three': 3}
>>> mykeys = ['three', 'one']
How can I use mykeys
to get the corresponding values in the dictionary as a list?
>>> mydict.WHAT_GOES_HERE(mykeys)
[3, 1]
解决方案
A list comprehension seems to be a good way to do this:
>>> [mydict[x] for x in mykeys]
[3, 1]
这篇关于Python字典:获取键列表的值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文