如何使用列表理解构建一个字典?

时间:2022-12-02 20:23:43

How do I build a dict using list comprehension?

如何使用列表理解构建一个字典?

I have two lists.

我有两个清单。

 series = [1,2,3,4,5]
 categories = ['A', 'B', 'A', 'C','B']

I want to build a dict where the categories are the keys.

我想建立一个类别是键的字典。

Thanks for your answers I'm looking to produce:

感谢您的回答我想要制作:

 {'A' : [1, 3], 'B' : [2, 5], 'C' : [4]}

Because the keys can't exist twice

因为密钥不能存在两次

5 个解决方案

#1


10  

You have to have a list of tuples. The tuples are key/value pairs. You don't need a comprehension in this case, just zip:

你必须有一个元组列表。元组是键/值对。在这种情况下你不需要理解,只需zip:

dict(zip(categories, series))

Produces {'A': 3, 'B': 5, 'C': 4} (as pointed out by comments)

产生{'A':3,'B':5,'C':4}(如评论所指出)

Edit: After looking at the keys, note that you can't have duplicate keys in a dictionary. So without further clarifying what you want, I'm not sure what solution you're looking for.

编辑:查看键后,请注意您不能在字典中使用重复键。因此,如果不进一步澄清您的需求,我不确定您正在寻找什么样的解决方案。

Edit: To get what you want, it's probably easiest to just do a for loop with either setdefault or a defaultdict.

编辑:为了得到你想要的东西,用setdefault或defaultdict做一个for循环可能是最容易的。

categoriesMap = {}
for k, v in zip(categories, series):
    categoriesMap.setdefault(k, []).append(v)

That should produce {'A': [1, 3], 'B': [2, 5], 'C': [3]}

这应该产生{'A':[1,3],'B':[2,5],'C':[3]}

#2


5  

from collectons import defaultdict

series = [1,2,3,4,5]
categories = ['A', 'B', 'A', 'C','B']

result = defaultdict(list)
for key, val in zip(categories, series)
    result[key].append(value)

#3


3  

Rather than being clever (I have an itertools solution I'm fond of) there's nothing wrong with a good, old-fashioned for loop:

而不是聪明(我有一个我喜欢的itertools解决方案)一个好的,老式的for循环没有错:

>>> from collections import defaultdict
>>> 
>>> series = [1,2,3,4,5]
>>> categories = ['A', 'B', 'A', 'C','B']
>>> 
>>> d = defaultdict(list)
>>> for c,s in zip(categories, series):
...     d[c].append(s)
... 
>>> d
defaultdict(<type 'list'>, {'A': [1, 3], 'C': [4], 'B': [2, 5]})

This doesn't use a list comprehension because a list comprehension is the wrong way to do it. But since you seem to really want one for some reason: how about:

这不使用列表理解,因为列表理解是错误的方法。但是因为你似乎真的想要一个出于某种原因:怎么样:

>> dict([(c0, [s for (c,s) in zip(categories, series) if c == c0]) for c0 in categories])
{'A': [1, 3], 'C': [4], 'B': [2, 5]}

That has not one but two list comprehensions, and is very inefficient to boot.

这不仅仅是两个列表推导,而且启动效率非常低。

#4


0  

In principle you can do as Kris suggested: dict(zip(categories, series)), just be aware that there can not be duplicates in categories (as in your sample code).

原则上你可以像克里斯建议的那样做:dict(zip(类别,系列)),只要知道类别中没有重复项(如示例代码中所示)。

EDIT :

编辑:

Now that you've clarified what you intended, this will work as expected:

既然你已经澄清了你的意图,这将按预期工作:

from collections import defaultdict
d = defaultdict(list)
for k, v in zip(categories, series):
    d[k].append(v)

#5


0  

d={ k:[] for k in categories }
map(lambda k,v: d[k].append(v), categories, series )

result:

结果:

d is now = {'A': [1, 3], 'C': [4], 'B': [2, 5]}

or (equivalent) using setdefault (thanks Kris R.)

或(等效)使用setdefault(感谢Kris R.)

d={}
map(lambda k,v: d.setdefault(k,[]).append(v), categories, series )

#1


10  

You have to have a list of tuples. The tuples are key/value pairs. You don't need a comprehension in this case, just zip:

你必须有一个元组列表。元组是键/值对。在这种情况下你不需要理解,只需zip:

dict(zip(categories, series))

Produces {'A': 3, 'B': 5, 'C': 4} (as pointed out by comments)

产生{'A':3,'B':5,'C':4}(如评论所指出)

Edit: After looking at the keys, note that you can't have duplicate keys in a dictionary. So without further clarifying what you want, I'm not sure what solution you're looking for.

编辑:查看键后,请注意您不能在字典中使用重复键。因此,如果不进一步澄清您的需求,我不确定您正在寻找什么样的解决方案。

Edit: To get what you want, it's probably easiest to just do a for loop with either setdefault or a defaultdict.

编辑:为了得到你想要的东西,用setdefault或defaultdict做一个for循环可能是最容易的。

categoriesMap = {}
for k, v in zip(categories, series):
    categoriesMap.setdefault(k, []).append(v)

That should produce {'A': [1, 3], 'B': [2, 5], 'C': [3]}

这应该产生{'A':[1,3],'B':[2,5],'C':[3]}

#2


5  

from collectons import defaultdict

series = [1,2,3,4,5]
categories = ['A', 'B', 'A', 'C','B']

result = defaultdict(list)
for key, val in zip(categories, series)
    result[key].append(value)

#3


3  

Rather than being clever (I have an itertools solution I'm fond of) there's nothing wrong with a good, old-fashioned for loop:

而不是聪明(我有一个我喜欢的itertools解决方案)一个好的,老式的for循环没有错:

>>> from collections import defaultdict
>>> 
>>> series = [1,2,3,4,5]
>>> categories = ['A', 'B', 'A', 'C','B']
>>> 
>>> d = defaultdict(list)
>>> for c,s in zip(categories, series):
...     d[c].append(s)
... 
>>> d
defaultdict(<type 'list'>, {'A': [1, 3], 'C': [4], 'B': [2, 5]})

This doesn't use a list comprehension because a list comprehension is the wrong way to do it. But since you seem to really want one for some reason: how about:

这不使用列表理解,因为列表理解是错误的方法。但是因为你似乎真的想要一个出于某种原因:怎么样:

>> dict([(c0, [s for (c,s) in zip(categories, series) if c == c0]) for c0 in categories])
{'A': [1, 3], 'C': [4], 'B': [2, 5]}

That has not one but two list comprehensions, and is very inefficient to boot.

这不仅仅是两个列表推导,而且启动效率非常低。

#4


0  

In principle you can do as Kris suggested: dict(zip(categories, series)), just be aware that there can not be duplicates in categories (as in your sample code).

原则上你可以像克里斯建议的那样做:dict(zip(类别,系列)),只要知道类别中没有重复项(如示例代码中所示)。

EDIT :

编辑:

Now that you've clarified what you intended, this will work as expected:

既然你已经澄清了你的意图,这将按预期工作:

from collections import defaultdict
d = defaultdict(list)
for k, v in zip(categories, series):
    d[k].append(v)

#5


0  

d={ k:[] for k in categories }
map(lambda k,v: d[k].append(v), categories, series )

result:

结果:

d is now = {'A': [1, 3], 'C': [4], 'B': [2, 5]}

or (equivalent) using setdefault (thanks Kris R.)

或(等效)使用setdefault(感谢Kris R.)

d={}
map(lambda k,v: d.setdefault(k,[]).append(v), categories, series )