从字典计算Python中的平均函数
我需要能够计算字典中权重的平均值。这也需要是一个函数。这是我的密码从字典计算Python中的平均函数,python,dictionary,average,Python,Dictionary,Average,我需要能够计算字典中权重的平均值。这也需要是一个函数。这是我的密码 animal=dict() animal['1']={'ID': '21012', 'plot':4, 'year':'1993', 'species': 'DM', 'weight': 42, 'hindfoot': 36, 'tag':'1EA0F9'} animal['2']={'ID':'22012', 'plot':4, 'year':'1995', 'species': 'DM', 'weight': 31, 'hi
animal=dict()
animal['1']={'ID': '21012', 'plot':4, 'year':'1993', 'species': 'DM', 'weight': 42, 'hindfoot': 36, 'tag':'1EA0F9'}
animal['2']={'ID':'22012', 'plot':4, 'year':'1995', 'species': 'DM', 'weight': 31, 'hindfoot': 37, 'tag':'0D373C'}
animal['3']={'ID': '23012', 'plot':17, 'year':'1996', 'species': 'DM', 'weight': 25, 'hindfoot': 37, 'tag':'64C6CC'}
animal['4']={'ID': '24012','plot':21, 'year':'1996', 'species': 'PP', 'weight': 26, 'hindfoot': 22, 'tag':'1F511A'}
animal['5']={'ID': '25012', 'plot':22, 'year':'1997', 'species': 'DM', 'weight': 53, 'hindfoot': 35, 'tag':'2624'}
animal['6']={'ID': '26012', 'plot':17, 'year':'1997', 'species': 'OT', 'weight': 14, 'hindfoot': 18, 'tag':'2863'}
animal['7']={'ID': '27012', 'plot':18, 'year':'1997', 'species': 'OT', 'weight': 23, 'hindfoot': 21, 'tag':'2913'}
animal['8']={'ID': '28012', 'plot':13, 'year':'1998', 'species': 'OT', 'weight': 36, 'hindfoot': 19, 'tag':'2997'}
animal['9']={'ID': '29012', 'plot':6, 'year':'1999', 'species': 'PM', 'weight': 20, 'hindfoot': 20, 'tag':'406'}
animal['10']={'ID': '30000', 'plot':14, 'year':'2000', 'species': 'DM', 'weight': 41, 'hindfoot': 34, 'tag':'156'}
result=dict()
def calc_avg(r):
if all(isinstance(x, numbers.Number) for x in r["weight"]):
print a["title"],":", sum(r["weight"])/float(len(r["weight"]))
return
import numbers
calc_avg(animal.items)
您的问题是,您假设您可以通过调用
r[“weight”]
(无论r
是什么)来生成动物体重列表-这不能简单地用python字典来完成。如果您想使用这种技术,您可以研究更复杂的数据结构,例如
但我假设你想继续使用你的字典。首先,我建议您传递整个dictionaryanimals
,而不是使用animal.items()
其次,您可以通过简单地调用isinstance(someObject,(int,long,float))
来测试对象是否是一个数字-这里的优点是针对数字进行测试。数字可以允许使用复数,而复数可能不是一个有效的权重(大概)-加号更清晰
将它们放在一起(由于代码中未包含[“title”]
,因此省略它):
def calc_平均值(r):
如果r.values()中x的所有值(isinstance(x[“weight”],(int,long,float)):
打印总和(x[“权重”]表示x的r值())/float(长度(r))
为了改进这一点,当一个简单的列表就足够时,没有理由需要一个字典来存储这些值。此外,您可以仅对具有有效权重的权重进行平均,而不是检查所有权重是否有效
animal = [
{'ID': '21012', 'plot':4, 'year':'1993', 'species': 'DM', 'weight': 42, 'hindfoot': 36, 'tag':'1EA0F9'},
{'ID':'22012', 'plot':4, 'year':'1995', 'species': 'DM', 'weight': 31, 'hindfoot': 37, 'tag':'0D373C'},
{'ID': '23012', 'plot':17, 'year':'1996', 'species': 'DM', 'weight': 25, 'hindfoot': 37, 'tag':'64C6CC'},
{'ID': '24012','plot':21, 'year':'1996', 'species': 'PP', 'weight': 26, 'hindfoot': 22, 'tag':'1F511A'},
{'ID': '25012', 'plot':22, 'year':'1997', 'species': 'DM', 'weight': 53, 'hindfoot': 35, 'tag':'2624'},
{'ID': '26012', 'plot':17, 'year':'1997', 'species': 'OT', 'weight': 14, 'hindfoot': 18, 'tag':'2863'},
{'ID': '27012', 'plot':18, 'year':'1997', 'species': 'OT', 'weight': 23, 'hindfoot': 21, 'tag':'2913'},
{'ID': '28012', 'plot':13, 'year':'1998', 'species': 'OT', 'weight': 36, 'hindfoot': 19, 'tag':'2997'},
{'ID': '29012', 'plot':6, 'year':'1999', 'species': 'PM', 'weight': 20, 'hindfoot': 20, 'tag':'406'},
{'ID': '30000', 'plot':14, 'year':'2000', 'species': 'DM', 'weight': 41, 'hindfoot': 34, 'tag':'156'}
]
def calc_avg(r):
valid_weights = [x["weight"] for x in r if isinstance(x["weight"], (int, long, float))]
print(sum(valid_weights) / float(len(valid_weights)))
calc_avg(animal)
您没有正确地遍历列表
您正在尝试遍历r['weight']
。没有r['weight']
。您传入了animal.items
(实际上应该是animal.items()
),它(如果调用函数)是字典中(键、值)
对的列表。我想实际上最好是传入animal.values()
,这只是值(这样就不会得到('0',{…})
)
如果您传入animal.values()
,您将得到一个字典列表。这些字典中的每一个都有一个'weight'
键,但是列表本身没有,因此您不能遍历r['weight']
。您需要做的是迭代r
本身。在迭代中得到的每个项目都有您要查找的密钥,因此它看起来像:
if all(isinstance(x['weight'], numbers.Number) for x in r):
avg = sum(x['weight'] for x in r) / float( len( r ) )
还有几个问题:
- 为什么有一个随机的
结果
记录你不使用
a['title']
来自哪里
- 我会将
导入
移到顶部,或者至少移到您定义计算平均值
的上方
您遇到了什么问题?你的具体问题是什么?
if all(isinstance(x['weight'], numbers.Number) for x in r):
avg = sum(x['weight'] for x in r) / float( len( r ) )