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

该博客讲述了如何通过Python字典计算具有相同计数(count)的项目的平均价格。作者提供了从数据列表中创建一个映射,用于存储每个count的总价和数量,然后使用defaultdict来避免KeyError,并最终计算平均值的方法。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值