我是大魔王10

导航

 
思路:列表中元素,做字典的key值,
字典的key对应的values是出现的次数



def pin(t):

d=dict()###空字典
lenth=len(t)
for i in t:

if i not in d:
d[i]=1
else:
d[i]+=1

for line in d: ##算出key在列表中出现的频率
val=d[line]/lenth
print(line,'在列表中出现的评率为',(round(val,2))) ##round 是val是小数位2位

return d

t=[1,2,3,2,2,'h','a','hello','hello']
print(pin(t))
posted on 2019-10-24 11:25  我是大魔王10  阅读(1098)  评论(0)    收藏  举报