简体   繁体   English

遍历嵌套列表并计算元素的平均值

[英]Iterate through nested list and calculate the average values of elements

Using Riot's API, I'm developing an application that analyzes data from a players League of Legends match history. 使用Riot的API,我正在开发一个应用程序,该应用程序分析来自英雄联盟比赛历史的数据。


I have a list containing item names , and time of purchase (in seconds) 我有一个包含商品名称购买时间(以秒为单位)的列表

item_list =
[['Boots of Speed', 50], 
['Health Potion', 60], 
['Health Potion', 80],
['Dorans Blade', 120],  
['Dorans Ring', 180], 
['Dorans Blade', 200], 
['Dorans Ring', 210]]

I'm trying to convert this to a unique list of items containing the item name , and the average time it was purchased. 我正在尝试将其转换为包含商品名称商品 平均购买时间的商品的唯一列表

For this example, this is what I'm looking to convert my list into: 对于此示例,这就是我想要将列表转换为的内容:

['Boots of Speed', 50]
['Health Potion', 70]
['Dorans Blade', 160]
['Dorans Ring', 195]

My attempted solution was to create an empty dictionary, iterate through the list, set the dictionary keys as the item names, and the average time as the key value. 我尝试的解决方案是创建一个空字典,遍历列表,将字典键设置为项目名称,并将平均时间设置为键值。

dict = {}
for item in item_list:
    item_name = item[0]
    time_of_purchase = item[1]
    dict[item_name] = (dict[item_name] + time_of_purchase) / 2 # Would cast this as an integer

The problem with this is that I'll be trying to perform calculations on a variable dict[item_name] before it is initialized. 问题在于,在初始化变量dict [item_name]之前,我将尝试对其进行计算。


At this point I am a bit stuck. 在这一点上,我有点卡住了。 Any pointers or help would be greatly appreciated. 任何指针或帮助将不胜感激。

You could use setdefault : 您可以使用setdefault

item_list = [['Boots of Speed', 50],
             ['Health Potion', 60],
             ['Health Potion', 80],
             ['Dorans Blade', 120],
             ['Dorans Ring', 180],
             ['Dorans Blade', 200],
             ['Dorans Ring', 210]]

result = {}
for item, count in item_list:
    result.setdefault(item, []).append(count)

print([[key, sum(value) / len(value) ] for key, value in result.items()])

Or as an alternative use defaultdict from the collections module: 或者,也可以使用collections模块中的defaultdict

from collections import defaultdict

item_list = [['Boots of Speed', 50],
             ['Health Potion', 60],
             ['Health Potion', 80],
             ['Dorans Blade', 120],
             ['Dorans Ring', 180],
             ['Dorans Blade', 200],
             ['Dorans Ring', 210]]

result = defaultdict(list)
for item, count in item_list:
    result[item].append(count)

print([[key, sum(value) / len(value) ] for key, value in result.items()])

Output 输出量

[['Dorans Blade', 160.0], ['Boots of Speed', 50.0], ['Health Potion', 70.0], ['Dorans Ring', 195.0]]

I would fill in the dictionary first, and for each item_name I would have a list of time_of_purchase values. 我将首先填写字典,对于每个item_name我都会有一个time_of_purchase值列表。 Once done, I would go through the dictionary (key,list) pairs, and calculate the average for each list. 完成后,我将遍历字典(键,列表)对,并计算每个列表的平均值。

item_list = [['Boots of Speed', 50],
['Health Potion', 60],
['Health Potion', 80],
['Dorans Blade', 120],
['Dorans Ring', 180],
['Dorans Blade', 200],
['Dorans Ring', 210]]

# Fill the dictionary
d = {}
for item in item_list:
    item_name, time_of_purchase = item
    if item_name not in d:
        d[item_name] = []
    d[item_name].append(time_of_purchase)

# Now calculate and print the average
retlist = []
for item_name, list_of_times in d.items():
    new_entry = [
        item_name,
        sum(list_of_times) // len(list_of_times),
    ]
    retlist.append(new_entry)
print retlist

Daniel's solution does the same, in a more pythonic and efficient way. 丹尼尔(Daniel)的解决方案以更加Python化和高效的方式完成了同样的工作。

There's two problems with your approach, the one you identify, and that if the item occurs three times, the average is not calculated correctly. 您的方法有两个问题,一个是您确定的,另一个是如果该项目出现3次,则平均值计算不正确。 To fix this, one approach is to sum the times, but also record the number of occurrences separately, then calculate the average as a second step. 要解决此问题,一种方法是对时间求和,但也要单独记录发生的次数,然后计算平均值作为第二步。

item_list = [['Boots of Speed', 50],
['Health Potion', 60],
['Health Potion', 80],
['Dorans Blade', 120],
['Dorans Ring', 180],
['Dorans Blade', 200],
['Dorans Blade', 200],
['Dorans Blade', 200],
['Dorans Ring', 210]]

item_dict = {}
for item in item_list:
    item_name = item[0]
    time_of_purchase = item[1]
    if (item_name in item_dict):
        # Add the duplicate item in
        item_dict[item_name] = item_dict[item_name][0] + time_of_purchase, item_dict[item_name][1] + 1
    else:
        # First time recording this item
        item_dict[item_name] = (time_of_purchase, 1)

for item_name in item_dict.keys():
    purchase_time = item_dict[item_name][0]
    purchase_count= item_dict[item_name][1]
    print("%-15s - %u" % (item_name, purchase_time/purchase_count))

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

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