描述
txt = '''Ifthereisonlyoneargument,itmustbeadictionarymappingUnicode |ordinals(integers)orcharacterstoUnicodeordinals,stringsorNone. |Characterkeyswillbethenconvertedtoordinals. |Iftherearetwoarguments,theymustbestringsofequallength,and |intheresultingdictionary,eachcharacterinxwillbemappedtothe |characteratthesamepositioniny.Ifthereisathirdargument,it |mustbeastring,whosecharacterswillbemappedtoNoneintheresult.''' 将以上字符串进行操作处理,编写程序完成统计: (1) 将字符串转换成列表; (2) 输入随机种子x,两个正整数m和n,要求: n > m,且 n 小于整个列表长度 (3) 使用shuffle函数将列表顺序打乱 (4) 统计输出m,n区间内,出现次数最多的前5个字符和次数。
样例
输入
1000 6 100
输出
n:13
i:11
e:11
a:7
s:6
import random
txt = '''Ifthereisonlyoneargument,itmustbeadictionarymappingUnicode |ordinals(integers)orcharacterstoUnicodeordinals,stringsorNone. |Characterkeyswillbethenconvertedtoordinals. |Iftherearetwoarguments,theymustbestringsofequallength,and |intheresultingdictionary,eachcharacterinxwillbemappedtothe |characteratthesamepositioniny.Ifthereisathirdargument,it |mustbeastring,whosecharacterswillbemappedtoNoneintheresult.'''
List = []
for i in txt:
List.append(i)
x, m, n = map(int, input().split())
random.seed(x)
random.shuffle(List)
List1 = List[m:n+1]
Dict1 = {}
for i in List1:
Dict1[i] = Dict1.get(i, 0) + 1
SortDict = sorted(Dict1.items(), key=lambda x: x[1], reverse=True)
for i in range(0, 5):
print("{}:{}".format(SortDict[i][0], SortDict[i][1]))