一:概述
在这里,将实现以及统计英文字符串中每个单词出现的次数的函数。欢迎点赞评论和关注哦!!!此实为书生训练模型的一个小任务。
二:具体说明
<1>在Vscode里面安装Python插件
安装的详细步骤截图如下所示:

即就是找到Vscode的插件搜索框界面,然后搜索python,下载如上图所示的python插件。用以完成统计英文字符串中每个单词出现的次数。
<2>wordcount函数的实现
<1>首先创建一个WordCount.py的文件

<2>开始书写相关代码,用以实现需求
详细实现代码如下:
# str = input("请输入英语字符串\n")
from itertools import count
text = """
Got this panda plush toy for my daughter's birthday,
who loves it and takes it everywhere. It's soft and
super cute, and its face has a friendly look. It's
a bit small for what I paid though. I think there
might be other options that are bigger for the
same price. It arrived a day earlier than expected,
so I got to play with it myself before I gave it
to her.
"""
# print(str)
def wordCount(text):
# 将字符串进行去除空格和符号并且将所有的字母变为小写
new_text = text.strip(",").strip("\n").strip(".").lower().split()
# 判断new_text的类型
print(type(new_text))
# print(new_text)
# 定义一个字典用来存储每个单词以及它们出现的次数key键值为单词,value值为单词出现的次数
word_dict = {}
# count = 0
# 利用循环遍历list集合
for word in new_text:
# 通过判断语句判断循环遍历取出的每一个word在字符串中出现的次数
# count = new_text.count(word)
if word in word_dict:
word_dict[word] += 1
else:
word_dict[word] = 1
# 打印每个单词以及出现的次数
print(word_dict)
# for word, count in word_dict.items():
# print(f"'{word}':'{count}'")
# print(word_dict)
if __name__ == '__main__':
wordCount(text)这个知识主要考察的是对python基础知识的应用。字符串、字典、条件判断以及循环的使用。
<3>上述实现代码的Debug(VScode版)
调试代码如下图所示:

运行之后看看后面几步的step over,代码的运行过程是如何的?



连续继续的箭头效果图如下所示:




运行之后,长度的变化截图如下所示:



到这里基本就完成了deg的过程。
















