yourtaxue 2021-11-02 18:53 采纳率: 100%
浏览 262
已结题

编写程序,统计下面英文短文中,每个单词出现的次数


article='In our world , one creature without any rivals is a lifeless creature. If a man lives without rivals, he is bound to be satisfied with the present and will not strive for the better. He would hold back before all difficulties and decline in inaction and laziness. Adverse environment tends to cultivate successful people. Therefore, your rivals are not your opponents or those you grudge. Instead , they are your good friends! In our lives, we need some rivals to "push us into the river", leaving us striving ahead in all difficulties and competitions. In our work, we need some rivals to be picky about us and supervise our work with rigorous requirements and standards. Due to our rivals, we can bring out our potential to the best; Due to our rivals, we will continuously promote our capabilities when competing with them!'
x=article.lower().strip().replace(',',' ').replace('.',' ').replace('"',' ').replace('"',' ').replace('!',' ').replace(';',' ').split()
print(x)
st=set(x)
dict={}
for i in st:
     y=x.count(i)
     dict[i]=y
dict=sorted(dict.items(),key=lambda x:x[1],reverse=True)
for i in range(len(st)):
  print(dict[i][0],'->',dict[i][1],end=' ')
  if(i%5==0):
     print('\n')

其他要求:(1)忽略大小写;(2)去除标点符号,不能在单词中出现标点或出现对标点的统计;(3)按词频由高到低的顺序输出统计结果,每个词及其统计结果显示时固定宽度并右对齐,每行显示5个单词的统计结果,总体要求整齐,方便查看
怎么显示时固定宽度并右对齐

  • 写回答

1条回答 默认 最新

  • Fioman_Hammer 2021-11-03 10:06
    关注
    
    article = 'In our world , one creature without any rivals is a lifeless creature. If a man lives without rivals, ' \
              'he is bound to be satisfied with the present and will not strive for the better. ' \
              'He would hold back before all difficulties and decline in inaction and laziness. Adverse environment tends to cultivate successful people. ' \
              'Therefore, your rivals are not your opponents or those you grudge. Instead , they are your good friends! In our lives, ' \
              'we need some rivals to "push us into the river", leaving us striving ahead in all difficulties and competitions. ' \
              'In our work, we need some rivals to be picky about us and supervise our work with rigorous requirements and standards. ' \
              'Due to our rivals, we can bring out our potential to the best; Due to our rivals, we will continuously promote our capabilities when competing with them!'
    import string
    
    data = article.lower().strip()
    for c in string.punctuation:
        data = data.replace(c," ") # 将标点符号转换成空格
    # 将换行符替换为空格
    data = data.replace("\n"," ")
    words = data.strip().split(" ")
    wordDict = {}
    for word in words:
        if word == "" or word == " ":
            continue
        if word not in wordDict:
            wordDict[word] = 1
        else:
            wordDict[word] = wordDict[word] + 1
    
    # 对字典,按照value进行排序
    resSorted = sorted(wordDict.items(),key=lambda x:x[1],reverse=True)
    
    # 先求出单词的最大长度,按照这个最大的长度去定每个单词的位置
    maxLen = len(max(wordDict.keys(),key=lambda x:len(x))) # 固定宽度按照最长的那个单词去定
    
    start = 0
    for word,count in resSorted:
        if start % 5 == 0 and start != 0:
            print()
        print("{:>{}} {}".format(word,maxLen,count),end=" ")
        start += 1
    

    img

    如果觉得答案对你有帮助,请点击下采纳,谢谢~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月12日
  • 已采纳回答 11月4日
  • 创建了问题 11月2日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。