在python中合并字典值列表 [英] Merging dictionary value lists in python
问题描述
我正在尝试合并三个字典,它们都有相同的键,以及值列表或单个值。
a = {'a':[1,2],'c':[5,6],'b':[3,4]}
two = {'a' 3.4],'c':[5.6,7.6],'b':[3.5,4.5]}
three = {'a':1.2,'c':3.4,'b':2.3}
我需要的是将值添加到一个列表中的所有项目。
result = {'a':[1,2,2.4,3.4,1.2],'c':[5,6,5.6, ,2.3],'b':[3,4,3.5,4.5,3.4]}
已尝试过几件事情,但大多数都将这些值放入嵌套列表中。
例如
out = dict((k,[one [k],two.get(k)
{'a':[[1,2],[2.4,3.4],1.2],'c':[[5,6],[5.6 ,7.6],3.4],'b':[[3,4],[3.5,4.5],2.3]}
我尝试通过循环遍历值来更新它:
out.update((k,[x for x in v])for k,v in out.iteritems())
但结果正好一样。
我试图简单地添加列表,但因为第三个字典只有一个浮点数,我无法做到这一点。
check = dict((k,[一[k] +二[k] +三[k]])在一个)
所以我试图先添加一个和两个值的列表,然后追加价值三。添加列表运行良好,但是当我尝试从第三个字典附加浮点数时,突然,整个值都转到无。
check = dict((k,[一[k] +二[k]])在一个)
{'a':[[1,2,2.4,3.4]],'c ':[[5,6,5.6,7.6]],'b':[[3,4,3.5,4.5]]}
new = dict((k,v.append(three [k] )for k,v in check.items())
{'a':无,'c':无,'b':无}
作为一个单行,有字典理解:
new = {key:value + two [key] + [three [key]] for key,value in one.iteritems()}
/ pre>
这将创建新的列表,将列表从
一个
与两个
,将三个
中的单个值放入临时列表中,以使连接更容易。
或 c循环更新
一个
就位:为key,value in one.iteritems():
value.extend(two [key])
value.append(three [key])
这使用
list.extend()
来更新原始列表就位于两个
和list.append()
中的列表中,从three
。
你出错了:
-
您的第一次尝试创建一个新列表,其值为
一个
,两个
和三个
嵌套在一起,而不是连接现有的列表。您尝试清理这些只是复制了这些嵌套列表。 -
您的第二次尝试不起作用,因为
/ code>不是列表,所以不能连接。我为这个值创建了一个新的列表。
-
最后一次尝试不应该使用
list.append()
,因为您存储该方法的返回值,始终为无
(其更改存储在v
直接,列表不需要再次返回)
演示第一种方法:
>>>一个= {'a':[1,2],'c':[5,6],'b':[3,4]}
>>>两个= {'a':[2.4,3.4],'c':[5.6,7.6],'b':[3.5,4.5]}
>>> 3 = {'a':1.2,'c':3.4,'b':2.3}
>>>对于键值,键值为{key + value [+] [3 [key]],值为one.iteritems()}
{'a':[1,2,2.4,3.4,1.2] ':[5,6,5.6,7.6,3.4],'b':[3,4,3.5,4.5,2.3]}
I'm trying to merge three dictionaries, which all have the same keys, and either lists of values, or single values.
one={'a': [1, 2], 'c': [5, 6], 'b': [3, 4]}
two={'a': [2.4, 3.4], 'c': [5.6, 7.6], 'b': [3.5, 4.5]}
three={'a': 1.2, 'c': 3.4, 'b': 2.3}
What I need is for all the items in the values to be added to one list.
result={'a': [1, 2, 2.4, 3.4, 1.2], 'c': [5, 6, 5.6, 7.6, 2.3], 'b': [3, 4, 3.5, 4.5, 3.4]}
I have tried several things, but most put the values into nested lists. E.g.
out=dict((k, [one[k], two.get(k), three.get(k)]) for k in one)
{'a': [[1, 2], [2.4, 3.4], 1.2], 'c': [[5, 6], [5.6, 7.6], 3.4], 'b': [[3, 4], [3.5, 4.5], 2.3]}
I tried updating it by looping through the values:
out.update((k, [x for x in v]) for k,v in out.iteritems())
but the results was exactly the same. I have tried to simply add the lists, but because the third dictionary has only a float, I couldn't do it.
check=dict((k, [one[k]+two[k]+three[k]]) for k in one)
So I tried to first add the lists in values of one and two, and then append the value of three. Adding the lists worked well, but then when I tried to append the float from the third dictionary, suddenly the whole value went to 'None'
check=dict((k, [one[k]+two[k]]) for k in one)
{'a': [[1, 2, 2.4, 3.4]], 'c': [[5, 6, 5.6, 7.6]], 'b': [[3, 4, 3.5, 4.5]]}
new=dict((k, v.append(three[k])) for k,v in check.items())
{'a': None, 'c': None, 'b': None}
As a one-liner, with a dictionary comprehension:
new = {key: value + two[key] + [three[key]] for key, value in one.iteritems()}
This creates new lists, concatenating the list from one
with the corresponding list from two
, putting the single value in three
into a temporary list to make concatenating easier.
Or with a for
loop updating one
in-place:
for key, value in one.iteritems():
value.extend(two[key])
value.append(three[key])
This uses list.extend()
to update original list in-place with the list from two
, and list.append()
to add the single value from three
.
Where you went wrong:
your first attempt creates a new list with the values from
one
,two
andthree
nested within rather than concatenating the existing lists. Your attempt to clean that up just copied those nested lists across.Your second attempt didn't work because the value in
three
is not a list so could not be concatenated. I created a new list just for that one value.Your last attempt should not have used
list.append()
in a generator expression, because you store the return value of that method, which is alwaysNone
(its change is stored inv
directly and the list doesn't need returning again).
Demo of the first approach:
>>> one={'a': [1, 2], 'c': [5, 6], 'b': [3, 4]}
>>> two={'a': [2.4, 3.4], 'c': [5.6, 7.6], 'b': [3.5, 4.5]}
>>> three={'a': 1.2, 'c': 3.4, 'b': 2.3}
>>> {key: value + two[key] + [three[key]] for key, value in one.iteritems()}
{'a': [1, 2, 2.4, 3.4, 1.2], 'c': [5, 6, 5.6, 7.6, 3.4], 'b': [3, 4, 3.5, 4.5, 2.3]}
这篇关于在python中合并字典值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!