python字典求平均值,python中的平均字典列表

I have a list of dictionary like this

data = [{'Name': 'john', 'Height': 176, 'Weight':62, 'IQ':120,..},...]

the .. signifies various other integer/float attributes and all elements of the list has same format..

I want to get the average Height, Weight and all other numerical attributes in the cleanest/easiest way.

I could not come up with a better way than normal for looping which was too messy... and i don't want to use any external packages

解决方案

You can use the following

sum([item['Weight'] for item in data])/len(data)

and use

float(len(data))

if you want a more exact value.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要计算Python字典值的平均值,可以按照以下步骤进行操作: 1. 首先,从字典获取需要计算平均值的数值列表。你可以使用列表推导式来实现这一步骤,并将结果存储在一个列表。例如,如果你的字典名为`dictionary_database_list`,并且你要计算`average`键对应值的平均值,你可以使用以下代码: ``` avg_list = [value["average"] for value in dictionary_database_list] ``` 这将创建一个包含所有`average`键对应值的列表`avg_list`。 2. 接下来,你需要计算这个列表平均值。你可以使用内置函数`sum()`和`len()`来计算列表所有元素的总和和元素的数量,并将两者相除得到平均值。例如,你可以使用以下代码计算平均值: ``` average = sum(avg_list) / len(avg_list) ``` 这将给出一个名为`average`的变量,它存储了列表`avg_list`的平均值。 请注意,这里的`avg_list`是根据字典的键值对应关系得到的,你可以根据实际情况修改代码以适应你的字典结构。同时,你还可以根据需要对计算平均值的列表进行排序、筛选等操作。如果你需要对字典的多个键进行平均值计算,你可以使用类似的方法对每个键进行处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [python字典求平均值_Python字典 - 从其他值查找平均值](https://blog.csdn.net/weixin_39734987/article/details/109926049)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [python字典平均值_获取字典列表值的平均值](https://blog.csdn.net/weixin_36209301/article/details/113672069)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值