Python 字典:数字频数统计

数据统计 #类似哈姆雷特词频统计
描述
输入两个整数,在这两个整数组成的闭区间范围内生成100个随机整数,并统计出现数据的次数,出现0次的数字不输出(而不是输出0)。为满足评测需要,程序必须使用seed函数将随机种子设为10,并使用randint函数生成随机数。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

输入格式
一行当中输入两个整数,以空格间隔。题目保证两个整数从小到大‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

输出格式
按照生成随机数从小到大的顺序,每行输出一个生成的整数以及其出现的次数,以空格间隔。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

输入输出示例
输入 输出
示例 1
3 5‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

3 36
4 39
5 25

#补充1:

>>> a,b=input().split()
2 3
>>> a,b
('2', '3')
>>> type(a)
<class 'str'>
>>> c,d=map(int,input().split())
2 3
>>> c,d
(2, 3)
>>> type(c)
<class 'int'>
>>> 

#补充2:

from random import *
seed(10)

a,b=map(int,input().split())

count={
     }

for i in range(100):
    t=randint(a,b)
    count[t]=count.get(t,0)+1
    
x=list(count)
x.sort()
print(x)

运行:

3 5
[3, 4, 5]
>>> 

代码:

from random import *
seed(10)

a,b=map(int,input().split())

count={
     }

for i in range(100):
    t=randint(a,b)
    count[t]=count.get(t,0)+1
    
x=list(count.items())
x.sort(key=lambda x:x[0])

for m,n in x:
    print(m,n)

你可能感兴趣的:(Python)