西南科技大学 Python程序设计 字符统计

描述

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]))
    
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值