简体   繁体   English

我如何总结字典中具有相同索引的所有值,其中每个键都有一个嵌套列表作为值?

[英]How can i sum up all values with the same index in a dictionary which each key has a nested list as a value?

I have a dictionary, each key of dictionary has a list of list (nested list) as its value.我有一本字典,字典的每个键都有一个列表列表(嵌套列表)作为其值。 What I want is imagine we have:我想要的是想象我们有:

x = {1: [[1, 2], [3, 5]], 2: [[2, 1], [2, 6]], 3: [[1, 5], [5, 4]]}

My question is how can I access each element of the dictionary and concatenate those with same index: for example first list from all keys:我的问题是如何访问字典的每个元素并连接具有相同索引的元素:例如所有键的第一个列表:

[1,2] from first keye + 
[2,1] from second and 
[1,5] from third one 

How can I do this?我怎样才能做到这一点?

You can access your nested list easily when you're iterating through your dictionary and append it to a new list and the you apply the sum function.当您遍历字典并将 append 迭代到新列表并应用sum function 时,您可以轻松访问嵌套列表。

Code:代码:

x={1: [[1,2],[3,5]] , 2:[[2,1],[2,6]], 3:[[1,5],[5,4]]}
ans=[]
for key in x:
    ans += x[key][0]
print(sum(ans))

Output: Output:

12

Assuming you want a list of the first elements, you can do:假设您想要第一个元素的列表,您可以执行以下操作:

>>> x={1: [[1,2],[3,5]] , 2:[[2,1],[2,6]], 3:[[1,5],[5,4]]}
>>> y = [a[0] for a in x.values()]
>>> y
[[1, 2], [2, 1], [1, 5]]

If you want the second element, you can use a[1] , etc.如果你想要第二个元素,你可以使用a[1]等。

The output you expect is not entirely clear (do you want to sum? concatenate?), but what seems clear is that you want to handle the values as matrices.您期望的 output 并不完全清楚(您想求和吗?连接?),但似乎很清楚的是您想将值作为矩阵处理。

You can use numpy for that:您可以为此使用numpy

summing the values对值求和
import numpy as np

sum(map(np.array, x.values())).tolist()

output: output:

[[4, 8], [10, 15]]        # [[1+2+1, 2+1+5], [3+2+5, 5+6+4]]
concatenating the matrices (horizontally)连接矩阵(水平)
import numpy as np

np.hstack(list(map(np.array, x.values()))).tolist()

output: output:

[[1, 2, 2, 1, 1, 5], [3, 5, 2, 6, 5, 4]]

As explained in How to iterate through two lists in parallel?如何并行遍历两个列表中所述? , zip does exactly that: iterates over a few iterables at the same time and generates tuples of matching-index items from all iterables. , zip正是这样做的:同时迭代几个迭代并从所有迭代生成匹配索引项的元组。

In your case, the iterables are the values of the dict.在您的情况下,迭代是字典的values So just unpack the values to zip :所以只需将值解压缩到zip

x = {1: [[1, 2], [3, 5]], 2: [[2, 1], [2, 6]], 3: [[1, 5], [5, 4]]}

for y in zip(*x.values()):
    print(y)

Gives:给出:

([1, 2], [2, 1], [1, 5])
([3, 5], [2, 6], [5, 4])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 嵌套字典:将字典中相同键的值的总和嵌套为字典值 - Nested dictionary: sum values of same key of the dictionaries nested as a value of dictionary 如何对同一个字典值同时使用键和索引? - How can I use both a key and an index for the same dictionary value? 如何从每个键的键列表中初始化 python 中的字典以具有相同的初始值? - How can I initialize a dictionary in python from a list of keys for each key to have the same initial value? 在python中,如何找到字典中的值之和? 每个键都有多个值 - In python, how do I find the sum of values in a dictionary? Where each key has multiple values 如何计算嵌套字典中每个键的列表长度总和? - how to calculate the sum of list's lengths of each key in a nested dictionary? 如何创建嵌套字典键并从命名空间键值对列表中为其分配值? - how can I create nested dictionary keys and assign them values from a list of namespaced key value pairs? 如何获取不同字典中相同键的所有值,并且字典存储在列表中 - how can i get all the values for the same key in different dictionary and dictionaries are stored in a list 具有相同键 PYTHON 的嵌套字典的总和值 - Sum values of a Nested Dictionary with same key PYTHON 使用numpy,如何生成一个数组,其中每个索引的值是从0到第二个数组中相同索引的值的总和? - Using numpy, how can I generate an array where the value at each index is the sum of the values from 0 to that same index in a second array? 我如何总结所有值取决于熊猫中的索引值? - how can i sum all values depend on index value in pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM