Python 将字典值映射到列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33078554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mapping dictionary value to list
提问by ulrich
Given the following dictionary:
给定以下字典:
dct = {'a':3, 'b':3,'c':5,'d':3}
How can I apply these values to a list such as:
如何将这些值应用于列表,例如:
lst = ['c', 'd', 'a', 'b', 'd']
in order to get something like:
为了得到类似的东西:
lstval = [5, 3, 3, 3, 3]
采纳答案by wim
Using map
:
使用map
:
>>> map(dct.get, lst)
[5, 3, 3, 3, 3]
Using a list comprehension:
使用列表理解:
>>> [dct[k] for k in lst]
[5, 3, 3, 3, 3]
回答by Eugene Soldatov
You can iterate keys from your list using map
function:
您可以使用map
函数从列表中迭代键:
lstval = list(map(dct.get, lst))
Or if you prefer list comprehension:
或者,如果您更喜欢列表理解:
lstval = [dct[key] for key in lst]
回答by Sanjay T. Sharma
You can use a list comprehension for this:
您可以为此使用列表理解:
lstval = [ dct.get(k, your_fav_default) for k in lst ]
I personally propose using list comprehensions over built-in map
because it looks familiar to all Python programmers, is easier to parse and extend in case a custom default value is required.
我个人建议使用列表推导而不是内置推导,map
因为它对所有 Python 程序员来说都很熟悉,而且在需要自定义默认值时更容易解析和扩展。
回答by luoluo
Do not use a dict
as variable name, as it was built in.
不要使用 adict
作为变量名,因为它是内置的。
>>> d = {'a':3, 'b':3,'c':5,'d':3}
>>> lst = ['c', 'd', 'a', 'b', 'd']
>>> map(lambda x:d.get(x, None), lst)
[5, 3, 3, 3, 3]
回答by khelwood
lstval = [d[x] for x in lst]
Don't name your dictionary dict
. dict
is the name of the type.
不要命名你的字典dict
。dict
是类型的名称。
回答by TobiMarg
I would use a list comprehension:
我会使用列表理解:
listval = [dict.get(key, 0) for key in lst]
The .get(key, 0)
part is used to return a default value (in this case 0) if no element with this key exists in dict
.
该.get(key, 0)
部分用于返回缺省值(在本例中为0)如果与此键的元素存在于dict
。
回答by Olivier Pirson
In documentation of Python 3:
在 Python 3 的文档中:
dict.items()
"Return a new view of the dictionary's items ((key, value) pairs)" https://docs.python.org/3/library/stdtypes.html#dict.items- "
zip()
in conjunction with the * operator can be used to unzip a list" https://docs.python.org/3/library/functions.html#zip
dict.items()
“返回字典项目((键,值)对)的新视图” https://docs.python.org/3/library/stdtypes.html#dict.items- "
zip()
与 * 运算符一起可用于解压缩列表" https://docs.python.org/3/library/functions.html#zip
So, zip(*d.items())
give your result.
所以,zip(*d.items())
给出你的结果。
d = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
print(d.items()) # [('a', 1), ('c', 3), ('b', 2), ('d', 4)] in Python 2
# dict_items([('a', 1), ('c', 3), ('b', 2), ('d', 4)]) in Python 3
print(zip(*d.items())) # [('a', 1), ('c', 3), ('b', 2), ('d', 4)] in Python 2
# <zip object at 0x7f1f8713ed40> in Python 3
k, v = zip(*d.items())
print(k) # ('a', 'c', 'b', 'd')
print(v) # (1, 3, 2, 4)
回答by Tim Skov Jacobsen
Mapping dictionary values to a nested list
将字典值映射到嵌套列表
The question has already been answered by many. However, no one has mentioned a solution in case the list is nested.
这个问题已经有很多人回答了。但是,没有人提到过嵌套列表的解决方案。
By changing the list in the original question to a list of lists
通过将原始问题中的列表更改为列表列表
dct = {'a': 3, 'b': 3, 'c': 5, 'd': 3}
lst = [['c', 'd'], ['a'], ['b', 'd']]
The mapping can be done by a nested list comprehension
映射可以通过嵌套列表理解来完成
lstval = [[dct[e] for e in lst[idx]] for idx in range(len(lst))]
# lstval = [[5, 3], [3], [3, 3]]