简体   繁体   中英

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.

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?

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 . 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:

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.

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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