有没有会python的学霸啊?怎么随机从1-6中选1000个数,然后统计次数啊
发布网友
发布时间:2022-04-18 21:04
我来回答
共2个回答
热心网友
时间:2022-04-18 22:33
import random
l = [random.choice([1,2,3,4,5,6]) for _ in range(1000)]
d = [(i, l.count(i)) for i in set(l)]
print d
热心网友
时间:2022-04-18 23:51
>>> import random
>>> import collections
>>>
>>> lst = range(1,7)
>>> serial = [random.choice(lst) for x in xrange(1000)]
>>> counter = collections.Counter(serial)
>>> counter.most_common()
[(4, 190), (5, 178), (3, 169), (6, 169), (1, 149), (2, 145)]
>>>