我如何将一个命令列表合并到一个命令中?

时间:2022-02-18 18:05:54

How can I turn a list of dicts like this

我怎样才能把这些口头禅列出来呢

[{'a':1}, {'b':2}, {'c':1}, {'d':2}]

Into a single dict like this

变成这样的一个法令

{'a':1, 'b':2, 'c':1, 'd':2}

9 个解决方案

#1


92  

This works for dictionaries of any length:

这适用于任何长度的字典:

>>> result = {}
>>> for d in L:
...    result.update(d)
... 
>>> result
{'a':1,'c':2,'b':1,'d':2}

And as generator-oneliner:

正如generator-oneliner:

dict(pair for d in L for pair in d.items())

In Python 2.7 and 3.x this can and should be written as dict comprehension (thanks, @katrielalex):

在Python 2.7和3中。这个可以也应该写成dict comprehension(谢谢@katrielalex):

{ k: v for d in L for k, v in d.items() }

#2


26  

In case of Python 3.3+, there is a ChainMap collection:

对于Python 3.3+,有一个链映射集合:

>>> from collections import ChainMap
>>> a = [{'a':1},{'b':2},{'c':1},{'d':2}]
>>> dict(ChainMap(*a))
{'b': 2, 'c': 1, 'a': 1, 'd': 2}

Also see:

还看到:

#3


5  

>>> L=[{'a': 1}, {'b': 2}, {'c': 1}, {'d': 2}]    
>>> dict(i.items()[0] for i in L)
{'a': 1, 'c': 1, 'b': 2, 'd': 2}

Note: the order of 'b' and 'c' doesn't match your output because dicts are unordered

注意:'b'和'c'的顺序与您的输出不匹配,因为命令是无序的

if the dicts can have more than one key/value

如果dicts可以有多个键/值

>>> dict(j for i in L for j in i.items())

#4


3  

For flat dictionaries you can do this:

对于平面词典,你可以这样做:

from functools import reduce
reduce(lambda a, b: dict(a, **b), list_of_dicts)

#5


1  

>>> dictlist = [{'a':1},{'b':2},{'c':1},{'d':2, 'e':3}]
>>> dict(kv for d in dictlist for kv in d.iteritems())
{'a': 1, 'c': 1, 'b': 2, 'e': 3, 'd': 2}
>>>

Note I added a second key/value pair to the last dictionary to show it works with multiple entries. Also keys from dicts later in the list will overwrite the same key from an earlier dict.

注意,我在最后一个字典中添加了第二个键/值对,以显示它可以处理多个条目。此外,列表后面的dicts的键也会从早期的命令中覆盖相同的键。

#6


1  

dict1.update( dict2 )

This is asymmetrical because you need to choose what to do with duplicate keys; in this case, dict2 will overwrite dict1. Exchange them for the other way.

这是不对称的因为你需要选择如何处理重复的键;在本例中,dict2将重写dict1。换种方式交换。

EDIT: Ah, sorry, didn't see that.

编辑:啊,对不起,我没看到。

It is possible to do this in a single expression:

可以在一个表达式中这样做:

>>> from itertools import chain
>>> dict( chain( *map( dict.items, theDicts ) ) )
{'a': 1, 'c': 1, 'b': 2, 'd': 2}

No credit to me for this last!

这最后一点对我来说是不值得称赞的!

However, I'd argue that it might be more Pythonic (explicit > implicit, flat > nested ) to do this with a simple for loop. YMMV.

但是,我认为使用一个简单的for循环可能更符合python(显式>隐式、扁平>嵌套)。YMMV。

#7


0  

You can use join function from funcy library:

您可以使用函数库中的连接函数:

from funcy import join
join(list_of_dicts)

#8


0  

dic1 = {'Maria':12, 'Paco':22, 'Jose':23} dic2 = {'Patricia':25, 'Marcos':22 'Tomas':36}

dic1 = {“帕科”:“玛丽亚”:12日,22日,“穆”:23 } dic2 = {帕特丽夏:25岁的“马科斯”:22“托马斯”:36 }

dic2 = dict(dic1.items() + dic2.items())

dic2 = dict(dic1.items() + dic2.items()

and this will be the outcome:

这就是结果:

dic2 {'Jose': 23, 'Marcos': 22, 'Patricia': 25, 'Tomas': 36, 'Paco': 22, 'Maria': 12}

第2{‘Jose’:23,‘Marcos’:22,‘Patricia’:25,‘Tomas’:36,‘Paco’:22,‘Maria’:12}

#9


0  

This is similar to @delnan but offers the option to modify the k/v (key/value) items and I believe is more readable:

这类似于@delnan,但提供了修改k/v (key/value)项的选项,我认为更容易读懂:

new_dict = {k:v for list_item in list_of_dicts for (k,v) in list_item.items()}

for instance, replace k/v elems as follows:

例如更换k/v elems如下:

new_dict = {str(k).replace(" ","_"):v for list_item in list_of_dicts for (k,v) in list_item.items()}

unpacks the k,v tuple from the dictionary .items() generator after pulling the dict object out of the list

从字典.items()生成器中将dict对象从列表中取出后,从字典.items()生成器中解包k、v tuple

#1


92  

This works for dictionaries of any length:

这适用于任何长度的字典:

>>> result = {}
>>> for d in L:
...    result.update(d)
... 
>>> result
{'a':1,'c':2,'b':1,'d':2}

And as generator-oneliner:

正如generator-oneliner:

dict(pair for d in L for pair in d.items())

In Python 2.7 and 3.x this can and should be written as dict comprehension (thanks, @katrielalex):

在Python 2.7和3中。这个可以也应该写成dict comprehension(谢谢@katrielalex):

{ k: v for d in L for k, v in d.items() }

#2


26  

In case of Python 3.3+, there is a ChainMap collection:

对于Python 3.3+,有一个链映射集合:

>>> from collections import ChainMap
>>> a = [{'a':1},{'b':2},{'c':1},{'d':2}]
>>> dict(ChainMap(*a))
{'b': 2, 'c': 1, 'a': 1, 'd': 2}

Also see:

还看到:

#3


5  

>>> L=[{'a': 1}, {'b': 2}, {'c': 1}, {'d': 2}]    
>>> dict(i.items()[0] for i in L)
{'a': 1, 'c': 1, 'b': 2, 'd': 2}

Note: the order of 'b' and 'c' doesn't match your output because dicts are unordered

注意:'b'和'c'的顺序与您的输出不匹配,因为命令是无序的

if the dicts can have more than one key/value

如果dicts可以有多个键/值

>>> dict(j for i in L for j in i.items())

#4


3  

For flat dictionaries you can do this:

对于平面词典,你可以这样做:

from functools import reduce
reduce(lambda a, b: dict(a, **b), list_of_dicts)

#5


1  

>>> dictlist = [{'a':1},{'b':2},{'c':1},{'d':2, 'e':3}]
>>> dict(kv for d in dictlist for kv in d.iteritems())
{'a': 1, 'c': 1, 'b': 2, 'e': 3, 'd': 2}
>>>

Note I added a second key/value pair to the last dictionary to show it works with multiple entries. Also keys from dicts later in the list will overwrite the same key from an earlier dict.

注意,我在最后一个字典中添加了第二个键/值对,以显示它可以处理多个条目。此外,列表后面的dicts的键也会从早期的命令中覆盖相同的键。

#6


1  

dict1.update( dict2 )

This is asymmetrical because you need to choose what to do with duplicate keys; in this case, dict2 will overwrite dict1. Exchange them for the other way.

这是不对称的因为你需要选择如何处理重复的键;在本例中,dict2将重写dict1。换种方式交换。

EDIT: Ah, sorry, didn't see that.

编辑:啊,对不起,我没看到。

It is possible to do this in a single expression:

可以在一个表达式中这样做:

>>> from itertools import chain
>>> dict( chain( *map( dict.items, theDicts ) ) )
{'a': 1, 'c': 1, 'b': 2, 'd': 2}

No credit to me for this last!

这最后一点对我来说是不值得称赞的!

However, I'd argue that it might be more Pythonic (explicit > implicit, flat > nested ) to do this with a simple for loop. YMMV.

但是,我认为使用一个简单的for循环可能更符合python(显式>隐式、扁平>嵌套)。YMMV。

#7


0  

You can use join function from funcy library:

您可以使用函数库中的连接函数:

from funcy import join
join(list_of_dicts)

#8


0  

dic1 = {'Maria':12, 'Paco':22, 'Jose':23} dic2 = {'Patricia':25, 'Marcos':22 'Tomas':36}

dic1 = {“帕科”:“玛丽亚”:12日,22日,“穆”:23 } dic2 = {帕特丽夏:25岁的“马科斯”:22“托马斯”:36 }

dic2 = dict(dic1.items() + dic2.items())

dic2 = dict(dic1.items() + dic2.items()

and this will be the outcome:

这就是结果:

dic2 {'Jose': 23, 'Marcos': 22, 'Patricia': 25, 'Tomas': 36, 'Paco': 22, 'Maria': 12}

第2{‘Jose’:23,‘Marcos’:22,‘Patricia’:25,‘Tomas’:36,‘Paco’:22,‘Maria’:12}

#9


0  

This is similar to @delnan but offers the option to modify the k/v (key/value) items and I believe is more readable:

这类似于@delnan,但提供了修改k/v (key/value)项的选项,我认为更容易读懂:

new_dict = {k:v for list_item in list_of_dicts for (k,v) in list_item.items()}

for instance, replace k/v elems as follows:

例如更换k/v elems如下:

new_dict = {str(k).replace(" ","_"):v for list_item in list_of_dicts for (k,v) in list_item.items()}

unpacks the k,v tuple from the dictionary .items() generator after pulling the dict object out of the list

从字典.items()生成器中将dict对象从列表中取出后,从字典.items()生成器中解包k、v tuple