Powder_Snow_ 2022-11-18 12:00 采纳率: 78.9%
浏览 27
已结题

python统计字符串中字母出现的频次,以字典形式呈现。

统计字符串中字母出现的频次,以字典形式呈现。
参考下面的例子,把空白处补充完整。

_______ ______   #导入re库函数

str1='''When I wrote the following pages, or rather the bulk of them, I lived alone, in the woods, a mile from any neighbor,  

in a house which I had built myself, on the shore of Walden Pond, in Concord, Massachusetts,  

and earned my living by the labor of my hands only.  

I lived there two years and two months. At preset I am a sojourner in civilized life again.'''

 

text=re.____(_____,_____,______)  #删除str1中的标点符号

dict1=_______    #定义一个空字典

for each in ______ :   #轮询text中的每个字符

   if each==' ' or each =='\n':   #如果是空格或者换行符,则忽略。

       continue

   elif each not in dict1.keys():

       ___________    

   else:

       ___________

print(dict1)

  • 写回答

4条回答 默认 最新

  • 7*24 工作者 2022-11-18 13:04
    关注
    
    import re
    str1='''When I wrote the following pages, or rather the bulk of them, I lived alone, in the woods, a mile from any neighbor,  
    in a house which I had built myself, on the shore of Walden Pond, in Concord, Massachusetts,  
    and earned my living by the labor of my hands only.  
    I lived there two years and two months. At preset I am a sojourner in civilized life again.'''
    text=re.sub('[.,]' ,' ',str1)  #删除str1中的标点符号
    # print(text)
    dict1= {}    #定义一个空字典
    for each in text :   #轮询text中的每个字符
       if each==' ' or each =='\n':   #如果是空格或者换行符,则忽略。
           continue
       elif each not in dict1.keys():
           dict1[each] = 1
       else:
           dict1[each] += 1
    print(dict1)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 11月26日
  • 已采纳回答 11月18日
  • 创建了问题 11月18日

悬赏问题

  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据