编写程序,生成包含1000个0到100之间的随机整数,并统计每个元素的出现次数(两种进行比较)

第一种
import random

y=[random.randint(0,100) for i in range(1000)]

z=set(y)

for i in z:
print(i,’:’,y.count(i))
//结果输出为有序排列的0~100这101个数以及其次数
第二种
import random

y = [random.randint(0,100) for i in range(1000)]

for i in y:
print(i,’:’,y.count(i))
//每输出一个随机数就会跟随输出其出现次数,输出一千个数的出现次数

猜你喜欢

转载自blog.csdn.net/qq_41542555/article/details/88371618