Python 将字典值列表替换为平均值

Python 将字典值列表替换为平均值,python,dictionary,Python,Dictionary,我有一本字典,比如说mydict,像这样: key1: list1 key2: list2 key3: list3 用平均值(避免循环)替换列表(值)的python方法是什么 在Python 3中,您可以从统计数据导入mean,并使用字典理解: >>> from statistics import mean >>> d = {'a':[1,2,3],'b':[4,5,6],'c':[7,8,9]} >>> d = {k:mean(v) fo

我有一本字典,比如说
mydict
,像这样:

key1: list1
key2: list2
key3: list3
用平均值(避免循环)替换列表(值)的python方法是什么


在Python 3中,您可以从
统计数据导入
mean
,并使用字典理解:

>>> from statistics import mean
>>> d = {'a':[1,2,3],'b':[4,5,6],'c':[7,8,9]}
>>> d = {k:mean(v) for k,v in d.items()}
>>> d
{'a': 2.0, 'c': 8.0, 'b': 5.0}

您的列表不是
int
float
的列表?请出示一些样品数据。
>>> from statistics import mean
>>> d = {'a':[1,2,3],'b':[4,5,6],'c':[7,8,9]}
>>> d = {k:mean(v) for k,v in d.items()}
>>> d
{'a': 2.0, 'c': 8.0, 'b': 5.0}