实现文本中的词频统计和排序`)
目录目录目录题目题干解析功能函数说明各部分代码功能分析选项解析选项A选项B选项C选项D知识点笔记1. 字符串方法join()的基本用法2. 列表方法append()的基本用法3. 字典方法values()的基本用法4. 函数list()的基本用法5. 函数sort()的基本用法1 基本排序2 降序排序3 使用key进行自定义排序① 按字符串长度排序② 按绝对值排序③ 按对象的属性排序4 综合使用key和reverse排序5 对元组列表排序6 与sorted()函数对比6. 字典项的引用与赋值题目题目来自https://edu.csdn.net/skill/practice/python-3-5/29?languagepythonmaterialId17346一个正式的项目一般都有对应的代码规范代码规范约定了如何在项目中编写代码。一般来说个人开发可以使用任何自己喜欢的代码规范但是在团队开发中一般要遵循团队约定的代码规范。不同的编程语言都有一些著名的代码规范。例如著名的KR是指《The C Programming Language》一书的作者Kernighan和Ritchie二人这是世界上第一本介绍C语言的书而KR风格即指他们在该书中书写代码所使用的风格。Google 有一个代码风格指引Google Style Guides里面包含了大部分主流编程语言的编码规范。以下Python三引号字符串摘录该规范的核心描述google_style_guide Every major open-source project has its own style guide: a set of conventions (sometimes arbitrary) about how to write code for that project. It is much easier to understand a large codebase when all the code in it is in a consistent style. “Style” covers a lot of ground, from “use camelCase for variable names” to “never use global variables” to “never use exceptions.” This project (google/styleguide) links to the style guidelines we use for Google code. If you are modifying a project that originated at Google, you may be pointed to this page to see the style guides that apply to that project. This project holds the C Style Guide, C# Style Guide, Swift Style Guide, Objective-C Style Guide, Java Style Guide, Python Style Guide, R Style Guide, Shell Style Guide, HTML/CSS Style Guide, JavaScript Style Guide, TypeScript Style Guide, AngularJS Style Guide, Common Lisp Style Guide, and Vimscript Style Guide. This project also contains cpplint, a tool to assist with style guide compliance, and google-c-style.el, an Emacs settings file for Google style. CodeChina上有一个中文镜像仓库zh-google-styleguide可以看到 Google Python 代码风格指引十分简短python_style_rules此外Python 官方代码风格文档是PEP8。你会问PEP是什么PEP是Python Enhancement Proposals的缩写。一个PEP是一份为Python社区提供各种增强功能的技术规格也是提交新特性以便让社区指出问题精确化技术文档的提案。实际的开发中可以通过配置开发环境的插件来辅助自动化检查代码风格。下面的Python三引号字符串描述了一组相关信息python_style_guides * Python 代码风格指南, * [google-python-styleguide_zh_cn](https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules /) * [PEP8](https://legacy.python.org/dev/peps/pep-0008/) * 代码风格和自动完成工具链 * 基本工具 * [pylint](https://pylint.org/) * [autopep8](https://pypi.org/project/autopep8/) * Visual Studio Code Python 开发基本插件 * Pylance * Python Path * Python-autopep8 请编写一段单词统计Python代码统计上述两个Python三引号字符串里英文单词的词频。要求单词请忽略大小写使用数组splits [\n, , -, :, /, *, _, (, ), , ”, “,],[,,,.,\n]来切割单词输出词频最高的5个单词和词频信息。基本代码框架如下# -*- coding: UTF-8 -*- def top_words(splits, text, top_n5): i 0 word_dict {} chars [] while i len(text): c text[i] if c in splits: # 过滤掉分隔字符串 while i1 len(text) and text[i1] in splits: i 1 word .join(chars).lower() # 统计词频 # TODO(You): 请在此添加代码 chars [] else: chars.append(c) i 1 word_list list(word_dict.values()) top_n min(top_n, len(word_list)) word_list.sort(keylambda word_info: word_info[count], reverseTrue) return word_list[0:top_n] if __name__ __main__: google_style_guide ... python_style_guides ... splits [ , -, :, /, *, _, (, ), , ”, “, ,, ., \n] tops top_words(splits, google_style_guidepython_style_guides) print(单词排行榜) print(--------) i 0 while i len(tops): top tops[i] word top[word] count top[count] print(f{i1}. 单词{word}, 词频{count}) i 1预期的输出结果为单词排行榜 -------- 1. 单词style, 词频23 2. 单词guide, 词频16 3. 单词to, 词频9 4. 单词python, 词频9 5. 单词project, 词频8以下选项是对代码中TODO部分的多种实现你能找出以下实现错误的选项吗Aword_info word_dict.get(word, {word: word, count: 0}) word_info[count] 1 word_dict[word] word_infoBword_info word_dict.get(word) if word_info is None: word_info {word: word, count: 1} word_dict[word] word_info else: word_info[count] 1Cword_info word_dict.get(word) if word_info is None: word_info {word: word, count: 0} word_dict[word] word_info word_info[count] 1Dif not word in word_dict: word_info {word: word, count: 0} word_dict[word] word_info word_info[count] 1题干解析该程序用于统计给定文本中出现频率最高单词。用户可以指定文本分隔符并获取出现次数最多的前 N 个单词。功能1.文本分割和单词提取根据指定的分隔符如空格、符号等将输入文本拆分为多个单词。2.词频统计计算每个单词在文本中出现的次数并以字典形式存储。3.词频排序按词频从高到低排序并返回词频最高的前 N 个单词。函数说明top_words(splits, text, top_n5)功能统计文本中出现频率最高的单词参数splits用于分割文本的字符列表列表中的每个字符都会被当作分隔符。text待处理的字符串文本。top_n返回的最高频单词数量默认为 5。main部分调用 top_words 函数给文本进行词频分析并将词频前五的单词打印到控制台。各部分代码功能分析1. 初始化变量i 0 word_dict {} chars []i 是一个计数器用于遍历文本中的每一个字符word_dict 是一个字典用于存储每个单词及其对应的出现次数。chars 是一个列表用于临时存储当前正在解析的单词的字符。2. 遍历文件while i len(text): c text[i] if c in splits: ... else: chars.append(c) i 1使用 while 循环遍历 text 中的每一个字符 c。如果当前字符 c 为分隔符即在 splits 列表中则表示当前单词解析结束进入下一步处理。否则将字符 c 添加到 chars 列表中继续解析单词。3. 处理单词及统计字频当遇到分隔符时表示一个单词已解析完成需要将其加入词典并统计出现次数。if c in splits: while i1 len(text) and text[i1] in splits: i 1 word .join(chars).lower() # 统计词频 # TODO(You): 请在此添加代码 chars []先处理连续出现分隔符的情况确保每个单词间的分割符只识别一次。使用 .join(chars).lower() 将收集到的字符合并成一个单词并将其转化成小写以确保统计时不区分大小写。统计词频是需要我们判断的部分TODO后面会一一解析每个选项。将 chars 列表清空为解析下一个单词做准备。4. 排序并返回结果word_list list(word_dict.values()) top_n min(top_n, len(word_list)) word_list.sort(keylambda word_info: word_info[count], reverseTrue) return word_list[0:top_n]将 word_dict 中所有的单词及其信息转换为一个列表 word_list。第二行代码确保返回的单词数量不超过文本中实际单词的数量。按照词频对 word_list 进行降序排序然后返回前 top_n 个频率最高的单词。5. 主程序google_style_guide ... python_style_guides ... splits [ , -, :, /, *, _, (, ), , ”, “] tops top_words(splits, google_style_guidepython_style_guides)设置检查目标文本和分隔符调用函数 top_words 获取单词统计列表。print(单词排行榜) print(--------) i 0 while i len(tops): top tops[i] word top[word] count top[count] print(f{i1}. 单词{word}, 词频{count}) i 1将词频前五的单词打印到控制台。选项解析选项Aword_info word_dict.get(word, {word: word, count: 0}) word_info[count] 1 word_dict[word] word_info第一行代码从词典 word_dict 中获取当前单词的信息若词典中没有该单词则初始化一个新的词典项 {word: word, count: 0}。将该单词的出现次数加1并更新到词典 word_dict 中。选项Bword_info word_dict.get(word) if word_info is None: word_info {word: word, count: 1} word_dict[word] word_info else: word_info[count] 1从词典 word_dict 中获取当前单词的信息。若词典中没有该单词则初始化一个新的词典项 {word: word, count: 1}并更新到词典 word_dict 中。键为 word值为 word_info 。若有则在词典项 word_info 中将该单词的出现次数 count 加1会同步更新词典 word_dict 中对应的单词计数。选项Cword_info word_dict.get(word) if word_info is None: word_info {word: word, count: 0} word_dict[word] word_info word_info[count] 1从词典 word_dict 中获取当前单词的信息。若词典中没有该单词则初始化一个新的词典项 {word: word, count: 0}并更新到词典 word_dict 中。键为 word值为 word_info 。在词典项 word_info 中将该单词的出现次数 count 加1会同步更新词典 word_dict 中对应的单词计数。选项Dif not word in word_dict: word_info {word: word, count: 0} word_dict[word] word_info word_info[count] 1这个选项无法实现正确的单词频率统计。原因在于 word_info 参数是在 if 语句的作用域内定义的但在 if 语句外的代码中可能会存在两种情况1. 如果 word 是第一次出现它的确会通过 if 语句被初始化然后 word_info[count] 1 可以成功执行。2. 但如果 word 已经存在与 word_dict 词典中word_info 变量在 if 语句内不会被重新赋值。此时word_info 变量可能指向的是上一个单词的字典项而不是当前单词的字典项。知识点笔记1. 字符串方法join()的基本用法在 Python 中join() 是一个字符串方法用于将一个可迭代对象如列表、元组、或字符串中的元素连接成一个字符串并在每个元素之间插入指定的分隔符。基本语法separator.join(iterable)separator 用作分隔符的字符串它将插入到 iterable 中的每个元素之间。iterable 一个包含字符串元素的可迭代对象如列表、元组、或字符串。常用功能1 将列表中的元素连接成字符串words [Python, is, fun] sentence .join(words) print(sentence) # 输出: Python is fun这里用空格 作为分隔符将列表中的单词连接成一个句子。2 连接元组中的元素numbers (1, 2, 3) result -.join(numbers) print(result) # 输出: 1-2-3使用连字符 - 作为分隔符将元组中的数字连接成一个字符串。3 连接字符串中的字符letters abc result ,.join(letters) print(result) # 输出: a,b,c在字符串的每个字符之间插入逗号。注意事项join() 方法要求 iterable 中的所有元素都必须是字符串类型否则会引发 TypeError 异常。separator 可以是任何字符串包括空字符串 本题中用法这将使元素直接连接在一起。2. 列表方法append()的基本用法在 Python 中append() 是列表方法用于在列表的末尾添加一个元素。这个方法非常常用尤其是在需要动态构建列表时。基本语法list.append(element)list 目标列表。element 要添加到列表末尾的元素。常用功能向列表末尾添加单个元素可以是不同类型的元素mixed_list [1, 2, 3] mixed_list.append(four) mixed_list.append([5, 6]) print(mixed_list) # 输出: [1, 2, 3, four, [5, 6]]你可以添加任何类型的元素包括字符串、数字甚至是另一个列表。注意事项append() 方法是就地修改列表的它不会返回新的列表而是直接在原列表上操作。只能将一个元素添加到列表末尾如果要添加多个元素可以使用循环或者其他方法如extend()。3. 字典方法values()的基本用法values() 是一个字典方法它返回一个包含字典中所有值的视图对象view object。这个视图对象包含了字典中每个键所对应的值而不包含键本身。假如 word_dict 如下word_dict { hello: {word: hello, count: 3}, world: {word: world, count: 2}, python: {word: python, count: 5} }执行 word_dict.values() 后会得到一个类似这样的视图对象dict_values([ {word: hello, count: 3}, {word: world, count: 2}, {word: python, count: 5} ])4. 函数list()的基本用法list() 是一个内置函数用于将任何可迭代对象转换为列表。当我们将 word_dict.values() 传递给 list() 函数时它会将这个视图对象转换为一个真正的列表。因此执行 list(word_dict.values()) 后会获得一个包含 word_dict 中所有值的列表word_list [ {word: hello, count: 3}, {word: world, count: 2}, {word: python, count: 5} ]5. 函数sort()的基本用法sort() 方法是 Python 中用于对列表进行原地排序的一个内置方法。常见用法及主要参数1 基本排序默认情况下sort() 会将列表中的元素按升序从小到大排序。numbers [3, 1, 4, 1, 5, 9] numbers.sort() print(numbers) # 输出: [1, 1, 3, 4, 5, 9]2 降序排序使用 reverseTrue 参数可以对列表进行排序。numbers [3, 1, 4, 1, 5, 9] numbers.sort(reverseTrue) print(numbers) # 输出: [9, 5, 4, 3, 1, 1]3 使用key进行自定义排序① 按字符串长度排序words [banana, apple, cherry, date] words.sort(keylen) print(words) # 输出: [date, apple, banana, cherry]这里 keylen 意味着按字符串的长度进行排序。② 按绝对值排序numbers [-6, 3, -2, 8, -1, 5] numbers.sort(keyabs) print(numbers) # 输出: [-1, -2, 3, 5, -6, 8]使用 keyabs 按元素的绝对值大小进行排序。③ 按对象的属性排序students [ {name: Alice, age: 25}, {name: Bob, age: 20}, {name: Charlie, age: 23} ] students.sort(keylambda student: student[age]) print(students) # 输出: [{name: Bob, age: 20}, {name: Charlie, age: 23}, {name: Alice, age: 25}]使用 keylambda student: student[age] 按照学生的年龄进行排序。4 综合使用key和reverse排序可以同时使用 key 和 reverse 来进行复杂排序例如按字符串长度降序排序words [banana, apple, cherry, date] words.sort(keylen, reverseTrue) print(words) # 输出: [banana, cherry, apple, date]5 对元组列表排序# 假设有一个包含元组的列表 pairs [(1, one), (2, two), (3, three), (4, four)] # 按照元组的第一个元素进行排序 pairs.sort() print(pairs) # 输出: [(1, one), (2, two), (3, three), (4, four)] # 按照元组的第二个元素进行排序 pairs.sort(keylambda pair: pair[1]) print(pairs) # 输出: [(4, four), (1, one), (3, three), (2, two)]6 与sorted()函数对比sort() 是对原列表进行原地排序排序后列表本身被修改。sorted() 返回一个新的排序后的列表原列表不变。numbers [3, 1, 4, 1, 5, 9] sorted_numbers sorted(numbers) print(sorted_numbers) # 输出: [1, 1, 3, 4, 5, 9] print(numbers) # 原列表未改变输出: [3, 1, 4, 1, 5, 9]6. 字典项的引用与赋值在 Python 中字典以及其他对象如列表是通过引用来存储和操作的。这意味着当你对一个字典的值如 word_info进行修改时如果这个值是从另一个字典如 word_dict中获取的那么修改会直接反映在原始字典中。具体来说word_info 变量存储的是对 word_dict[word] 值的引用而不是其副本。因此对 word_info 的任何修改都会直接影响到 word_dict [word]。# 示例字典 word_dict { hello: {word: hello, count: 1}, world: {word: world, count: 2} } # 获取字典中的某个项 word_info word_dict.get(hello) # 修改字典项 word_info[count] 1 # 检查原始字典的内容 print(word_dict)在这个例子中word_info 和 word_dict [hello] 是同一个对象。所以当 word_info [count] 被修改时word_dict [hello][count] 也会随之更新。