求出一组数:1080,750,1080,750,1080,850,960,2000,1250,1630,1080,1800,1080,2100,1080,1450,2500,560,1080,560中的众数及出现频率。众数指出现次数最多的数。请完善代码。

admin2019-09-02  27

问题 求出一组数:1080,750,1080,750,1080,850,960,2000,1250,1630,1080,1800,1080,2100,1080,1450,2500,560,1080,560中的众数及出现频率。众数指出现次数最多的数。请完善代码。
ls=[1080,750,1080,750,1080,850,960,2000,1250,1630,1080,\1800,1080,2100,1080,1450,2500,560,1080,560]
counts={}
for num in ls:
#此段代码请完善
items=list(counts.items())
items.sort(key=lambda x:[1],reverse=True)
num,count=items[0]
print(’’众数为{},出现频率为{}。’’.format(num,count))

选项

答案ls=[1080,750,1080,750,1080,850,960,2000,1250,1630,1080,1800,\1080,2100,1080,1450,2500,560,1080,560] counts={} for num in ls: counts[num]=counts.get(num,0)+1 items=list(counts.items()) items.sort(key=lambda x:[1],reverse=True) hum,count=items[0] print(’’众数为{},出现频率为{}。’’.format(num,count))t.left(90) tree(100,6)

解析
转载请注明原文地址:https://jikaoti.com/ti/vE50FFFM
0

最新回复(0)