python字典求平均值,Python字典 - 从其他值查找平均值

I have the following list

count = 3.5, price = 2500

count = 3, price = 400

count = 2, price = 3000

count = 3.5, price = 750

count = 2, price = 500

I want to find the average price for all where the count is the same. For example:

count = 2, price = 3000

count = 2, price = 500

3000 + 500 = 3500

3500/2 = 1750

Avg for 'count 2' is 1750

Here's my code so far

avg_list = [value["average"] for value in dictionary_database_list]

counter_obj = collections.Counter(count_list)

print ("AVG:")

for i in counter_obj:

print (i, counter_obj[i])

解决方案

I'll admit I'm not 100% clear on what you're looking for here, but I'll give it a shot:

A good strategy when you want to iterate over a list of "things" and accumulate some kind of information about "the same kind of thing" is to use a hash table. In Python, we usually use a dict for algorithms that require a hash table.

To collect enough information to get the average price for each item in your list, we need:

a) the total number of items with a specific "count"

b) the total price of items with a specific "count"

So let's build a data structure that maps a "count" to a dict containing "total items" and "total price" for the item with that "count".

Let's take our input in the format:

item_list = [

{'count': 3.5, 'price': 2500},

{'count': 3, 'price': 400},

{'count': 2, 'price': 3000},

{'count': 3.5, 'price': 750},

{'count': 2, 'price': 500},

]

Now let's map the info about "total items" and "total price" in a dict called items_by_count:

for item in item_list:

count, price = item['count'], item['price']

items_by_count[count]['total_items'] += 1

items_by_count[count]['total_price'] += price

But wait! items_by_count[count] will throw a KeyError if count isn't already in the dict. This is a good use case for defaultdict. Let's define the default value of a count we've never seen before as 0 total price, and 0 total items:

from collections import defaultdict

items_by_count = defaultdict(lambda: {

'total_items': 0,

'total_price': 0

})

Now our code won't throw an exception every time we see a new value for count.

Finally, we need to actually take the average. Let's get the information we need in another dict, mapping count to average price. This is a good use case for a dict comprehension:

{count: item['total_price'] / item['total_items']

for count, item in items_by_count.iteritems()}

This iterates over the items_by_count dict and creates the new dict that we want.

Putting it all together:

from collections import defaultdict

def get_average_price(item_list):

items_by_count = defaultdict(lambda: {

'total_items': 0,

'total_price': 0

})

for item in item_list:

count, price = item['count'], item['price']

items_by_count[count]['total_items'] += 1

items_by_count[count]['total_price'] += price

return {count: item['total_price'] / item['total_items']

for count, item in items_by_count.iteritems()}

If we pass in our example input dict, this function returns:

{3.5: 1625, 2: 1750, 3: 400}

Which is hopefully the output you want! Be cautious of gotchas like float division in your particular Python version.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值