迭代字典键值并以已知计数取平均值

时间:2018-11-19 14:42:15

标签: python python-3.7

作为python的初学者,我仍在尝试更好地理解字典计数/循环的概念。因此,我创建了一个样本数据来清楚地阐明我的问题。我在下面有一个无序的词典列表,其中包含一些商店的收入信息。我的问题是,如何根据这些商店报告的收入统计来获得每个商店的平均收入?

     #Given data 
     revenue_list_of_dictionaries:
     [{'shop_ID': 1,
       'revenue': 15000},
      {'shop_ID': 2,
       'revenue': 12000},
      {'shop_ID': 1,
       'revenue': 8500},
      {'shop_ID': 3,
       'revenue': 5000}
      {'shop_ID': 1,
       'revenue': 3500}]

     data = revenue_list_of_dictionaries

     result = {}

     # Executing code to find total revenue for each shop ID
     for revenue_list in data:
         shop_ID = revenue_list['shop_ID']
         revenue_per_month = revenue_list['revenue']
         if shop_ID not in result:
             result[shop_ID] = revenue_per_month
         else:
             result[shop_ID] += revenue_per_month

运行上面的代码会给我:

         {'1':27000,
          '2':12000,
          '3':5000}

基于每个商店的报告计数:

         {'1':3,
          '2':1,
          '3':1}

如何找到报告的每个收入的平均值,以便我的输出返回以下内容:

         {'1': 9000,
          '2': 12000,
          '3': 5000}

3 个答案:

答案 0 :(得分:1)

如果将总收入称为a,计数为b,并且它们都具有相同的键(必须使用相同的键),则可以使用dict理解来计算平均收入{{1 }}:

c

答案 1 :(得分:0)

如果您想操纵当前的工作方法,那么user8408080的方法就可以了。

否则,如@Chris_Rands的注释中所述,您可以使用collections.defaultdict()收集收入,将其存储为列表,然后在末尾计算平均值:

from collections import defaultdict
from statistics import mean

revenues = [
    {"shop_ID": 1, "revenue": 15000},
    {"shop_ID": 2, "revenue": 12000},
    {"shop_ID": 1, "revenue": 8500},
    {"shop_ID": 3, "revenue": 5000},
    {"shop_ID": 1, "revenue": 3500},
]

counts = defaultdict(list)
for revenue in revenues:
    counts[revenue['shop_ID']].append(revenue['revenue'])

print({k: mean(v) for k, v in counts.items()})
# {1: 9000, 2: 12000, 3: 5000}

您还可以拥有collections.Counter()中的collections.defaultdict(),并同时存储收入和计数:

from collections import defaultdict
from collections import Counter

revenues = [
    {"shop_ID": 1, "revenue": 15000},
    {"shop_ID": 2, "revenue": 12000},
    {"shop_ID": 1, "revenue": 8500},
    {"shop_ID": 3, "revenue": 5000},
    {"shop_ID": 1, "revenue": 3500},
]

counts = defaultdict(Counter)
for revenue in revenues:
    shp, rev = revenue['shop_ID'], revenue['revenue']
    counts[shp]['revenue'] += rev
    counts[shp]['count'] += 1

print({k: v['revenue'] / v['count'] for k, v in counts.items()})
# {1: 9000.0, 2: 12000.0, 3: 5000.0}

注意:在第一个示例中,我使用了statistic.median()来计算平均值。如果需要,也可以使用sum(v) / len(v)

答案 2 :(得分:0)

您可以在不使用python软件包的情况下进行操作,如下所示:

result = {}
CountList = []

for dic in revenue_list_of_dictionaries:
    result.setdefault(dic.values()[0], 0)
    result[dic.values()[0]] += dic.values()[1]
    CountList.append(dic.values()[0])

#returning an average of all shop_Id revenues
for k, value in result.items():
    result[k] = value/CountList.count(k)

print result
#{1: 9000, 2: 12000, 3: 5000}