幽兰丶 2022-08-21 23:17 采纳率: 100%
浏览 34
已结题

Python数据组合问题

使用字典实现以下程序:
用户首先输入一个英文段落,其中可能包含4种字符:字母、数字、空格、英文逗号和英文句号。
用户再次输入一个英文单词,输出:
段落一共有多少个单词
不同的单词有多少个
用户输入的单词出现多少次

计算并打印英文段落中这个单词的出现次数,不用区分大小写。

例如:
用户首先输入以下段落:
Python is an interpreted, interactive, object oriented programming language. It incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes. It supports multiple programming paradigms beyond object oriented programming, such as procedural and functional programming. Python combines remarkable power with very clear syntax. It has interfaces to many system calls and libraries, as well as to various window systems, and is extensible in C or C plus plus. It is also usable as an extension language for applications that need a programmable interface. Finally, Python is portable: it runs on many Unix variants including Linux and macOS, and on Windows.
用户再输入: python

输出:一共有103个单词,其中有72个不同的单词,python出现了3次。

  • 写回答

1条回答 默认 最新

  • 亖夕 Python领域新星创作者 2022-08-21 23:33
    关注

    img

    
    s = input("用户输入段落:")
    word = input("查找的单词:")
    s_li = s.replace(",", " ").replace(".", " ").lower().split()
    words_dict = {}
    for item in s_li:
        if item not in words_dict:
            words_dict[item] = 1
        else:
            words_dict[item] += 1
    print(f"一共有{sum(words_dict.values())}个单词, 其中有{len(words_dict)}个不同的单词,{word}出现了3次")
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 4月10日
  • 已采纳回答 4月2日
  • 创建了问题 8月21日

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢