Python编写程序,生成包含1000个0~100之间的随机整数,并统计每个元素的出现次数。(三种方法)

import random
#列表推导式产生满足条件的元素列表
aList = [random.randint(0,100) for i in range (1000)]
for i in aList:
    #i为列表元素值,aList.count(i)为出现次数
	print(i,':',aList.count(i))
import random
#列表推导式产生满足条件的元素列表
aList = [random.randint(0,100) for i in range (1000)]
#使用集合
s = set(aList)
for v in s:
    print(v, ':', aList.count(v))

import random
#列表推导式产生满足条件的元素列表
aList = [random.randint(0,100) for i in range (1000)]
#使用字典
d = dict()
for v in aList :
    d[v] = d.get(v ,0) + 1
for k, v in d.items():
    print(k, v, sep=':')

你可能感兴趣的:(Python,python,随机整数,出现次数,实现)