简体   繁体   English

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

[英]How to iterate through a bunch of values in a dictionaries that are lists python


ratinglist = open("ratings.txt").readlines()

booklist = open("booklist.txt").readlines()

keys = [key.strip('\n') for key in ratinglist[0::2]]

values = [value.strip(' \n').split(' ') for value in ratinglist[1::2]]


my_dict = dict(zip(keys, values))


for values in my_dict:
    value * x = sum

So, I have a giant dictionary that has a list of values for each key.所以,我有一个巨大的字典,其中包含每个键的值列表。

Here is an example of my dictionary:这是我的字典的一个例子:

'KeeLed': ['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'], 'Megan': ['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']}

What I need to do is do a for loop, that loops through each value, multiplies them, and then adds them.我需要做的是做一个 for 循环,循环遍历每个值,将它们相乘,然后将它们相加。

I just need help getting the for loop to iterate through the values correctly.我只需要帮助让 for 循环正确地遍历值。

For example, for KeeLed and Megan, I want this for loop to iterate through their seperate lists of values, and multiply them, then add them.例如,对于 KeeLed 和 Megan,我希望这个 for 循环遍历它们单独的值列表,然后将它们相乘,然后将它们相加。

Like this: 0 * 5, + 5 * 0, + 0 * 0, + 5 * 0.... etc etc像这样:0 * 5, + 5 * 0, + 0 * 0, + 5 * 0.... 等等

The issue is I haven't found anything online on how to iterate through lists that are inside a dictionary .问题是我没有在网上找到任何关于如何遍历字典中的列表的信息。 I've found some resources on looping through lists, but I need to loop through lists of values that are inside of a dictionary.我找到了一些关于循环列表的资源,但我需要循环访问字典中的值列表。

I would like the results to be put in a list, but I could do that if I get some help with the for loop.我希望将结果放在一个列表中,但如果我得到有关 for 循环的一些帮助,我可以这样做。

scores for each person = [325, 450]

"The issue is I haven't found anything online on how to iterate through lists that are inside a dictionary . I've found some resources on looping through lists, but I need to loop through lists of values that are inside of a dictionary." “问题是我没有在网上找到任何关于如何遍历字典中的列表的信息。我找到了一些关于遍历列表的资源,但我需要遍历字典中的值列表。 ”

EXAMPLE例子

myDict = {

'KeeLed': ['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'], 
'Megan': ['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']

}

# For-looping through dict will give the key.
for name in myDict:

  total = 0

  # applying the key into myDict will give the list associated with the dict
  for score in myDict[name]:

    total = total + int(score)

  print(name,total)

Result结果

KeeLed 54
Megan 85

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

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