import math
a = {0.234 : 100, 0.345 : 120, 2.45: 200, 2.55 : 250}
d = {}
for x in a:
if (math.floor(x)) not in d:
d[math.floor(x)] = [a[x]]
else:
d[math.floor(x)] += [a[x]]
import math
from collections import defaultdict
original_dict = {0.234 : 100, 0.345 : 120, 2.45: 200, 2.55 : 250}
d = defaultdict(list)
for k,v in original_dict.iteritems(): # or .items() if you are on Python 3.x
d[math.floor(k)].append(v)
for k,v in d.iteritems():
d[k] = sum(v)/len(v)
print(d)
d
的输出:然后:
d
的输出:defaultdict
用于收集基于公共键的值,公共键是原始dict中键的下限值相关问题 更多 >
编程相关推荐