幽兰丶 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日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来