简体   繁体   English

如何遍历字典中的一堆列表?

[英]How to iterate through a bunch of lists that are inside of a dictionary?

I have this giant dictionary, where I have a key for a key that is a person, and a list of values for each person, here is an example of two people inside of the dictionary:我有这本巨大的字典,其中有一个人的键的键,以及每个人的值列表,这是字典中两个人的示例:

{'NuNu': ['0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '3', '0', '3', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '3', '0', '0', '1', '0', '5', '0', '0', '0', '0', '0', '1', '3', '3', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], 'Hamza ALi': ['0', '0', '0', '0', '0', '0', '0', '-5', '0', '5', '1', '0', '0', '0', '1', '0', '5', '0', '-3', '0', '3', '0', '0', '0', '0', '0', '0', '-5', '0', '0', '-3', '5', '0', '5', '0', '3', '0', '0', '1', '0', '3', '1', '3', '5', '0', '0', '0', '0', '0', '1', '-5', '0', '0', '0', '0']}

Now, what I need to do is multiply each value together, like 0 * 0, 0 * 0, etc etc for the entire list of the values, and then add all of the values together.现在,我需要做的是将每个值相乘,例如 0 * 0、0 * 0 等,用于整个值列表,然后将所有值相加。

I have already done this in another piece of code, by just for two lists that I put the values inside for.我已经在另一段代码中完成了此操作,仅针对我将值放入其中的两个列表。

I have no idea how I would do it with a dictionary that has lists inside of it, that are values.我不知道如何使用其中包含列表(即值)的字典来执行此操作。

list1 = ['0', '0', '0', '5', '0', '0', '0', '5', '0', '5', '5', '0', '0', '0', '0', '0', '5', '-3', '-3', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '5', '0', '0', '5', '0', '5', '5', '5', '5', '0', '0', '0', '0', '0', '3', '1', '0', '0', '0', '0']
list2 = ['5', '5', '0', '0', '0', '0', '0', '0', '0', '3', '0', '5', '0', '0', '1', '0', '5', '0', '1', '5', '0', '0', '0', '0', '0', '1', '0', '5', '0', '0', '3', '5', '5', '0', '0', '5', '0', '0', '3', '0', '0', '3', '5', '5', '0', '0', '0', '0', '0', '5', '5', '0', '5', '0', '0']

int_list_1 = map(int,list1)
int_list_2 = map(int,list2)

third = map(sum, zip(int_list_1, int_list_2))

product = []

for num1, num2 in zip(int_list_1,int_list_2):
    product.append(num1 * num2)
x = sum(product)

print(x)

^ This gives me the correct value that I need, but I need help setting this piece of code up for multiple lists that are inside of a dictionary. ^ 这给了我所需的正确值,但我需要帮助为字典中的多个列表设置这段代码。

I know I need to have a for loop, maybe something like this?我知道我需要一个for循环,也许是这样的?

sum = 0
for value in dict:
    sum + y
print(sum)

This is my solution in general case.这是我在一般情况下的解决方案。 Suppose we have n items in dictionary d .假设我们在字典dn项目。 Then I make a collection of lists with integer values like so:然后我用整数值制作一个列表集合,如下所示:

vals = map(lambda x: [int(n) for n in x], d.values())

After that I get all the tuples of items that is going to be multiplied elementwise:之后,我得到了将要逐元素相乘的所有项目元组:

p = zip(*vals)

Finally, this is a way to calculate all the products:最后,这是一种计算所有产品的方法:

#from functools import reduce
products = [reduce(lambda x,y: x*y, n) for n in p]
output = sum(products)

Alternative way替代方式

Since lengths of dictionary values are balanced, we can use numpy actions:由于字典值的长度是平衡的,我们可以使用numpy操作:

import numpy as np
vals = np.array(list(d.values())).astype(int)
products = np.prod(vals, axis=0)
output = np.sum(products)

You try this.你试试这个。

a={'NuNu': ['0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '3', '0', '3', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '3', '0', '0', '1', '0', '5', '0', '0', '0', '0', '0', '1', '3', '3', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], 'Hamza ALi': ['0', '0', '0', '0', '0', '0', '0', '-5', '0', '5', '1', '0', '0', '0', '1', '0', '5', '0', '-3', '0', '3', '0', '0', '0', '0', '0', '0', '-5', '0', '0', '-3', '5', '0', '5', '0', '3', '0', '0', '1', '0', '3', '1', '3', '5', '0', '0', '0', '0', '0', '1', '-5', '0', '0', '0', '0']}
x,y=a.values()
x=map(int,x)
y=map(int,y)
out=[i*j for i,j in zip(x,y)]
print(out)
print(sum(out))

[0, 0, 0, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, -9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -9, 0, 0, 5, 0, 15, 0, 0, 0, 0, 0, 1, 9, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

37

You've got the right idea.你的想法是对的。 What about something like this for dicts with any number of keys.对于具有任意数量键的 dicts 来说,这样的事情怎么样。

def mul(iterable):
  t = 1
  for i in iterable:
     t *= int(i)
  return t

z = zip(*a.values()) # => [('0', '0'), ('0', '0'), ...]
m = map(mul, z)      # => [0, 0, ...]
print(sum(m))        # => 37

Edit编辑

You could replace the mul function with functools.reduce你可以用functools.reduce替换 mul 函数

from functools import reduce

z = zip(*a.values())
m = map(lambda t: reduce(lambda x, y: int(x) * int(y), t), z)
print(sum(m))

Now all as one line:现在全部作为一行:

print(sum(map(lambda t: reduce(lambda x, y: int(x) * int(y), t), zip(*a.values()))))

Another way is to use the reduce function:另一种方法是使用reduce函数:

from functools import reduce

inDict = {'NuNu': ['0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '3', '0', '3', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '3', '0', '0', '1', '0', '5', '0', '0', '0', '0', '0', '1', '3', '3', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], 'Hamza ALi': ['0', '0', '0', '0', '0', '0', '0', '-5', '0', '5', '1', '0', '0', '0', '1', '0', '5', '0', '-3', '0', '3', '0', '0', '0', '0', '0', '0', '-5', '0', '0', '-3', '5', '0', '5', '0', '3', '0', '0', '1', '0', '3', '1', '3', '5', '0', '0', '0', '0', '0', '1', '-5', '0', '0', '0', '0']}
result = sum([reduce(lambda x,y: x*y, map(int,elem)) for elem in zip(*inDict.values())])
print(result)

Output:输出:

37

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM