请实现一个wordcount函数,统计英文字符串中每个单词出现的次数。返回一个字典,key为单词,value为对应单词出现的次数。
AttributeError: 'dict' object has no attribute 'iteritems'
解决:python2.X不会报错,而Python3.X会报错,因为Python3.X中去除了iteritems()
将iteritems()替换为items()
Input:
"""Hello world! This is an example. Word count is fun. Is it fun to count words? Yes, it is fun!"""
#整理2
text="""Hello world!
This is an example.
Word count is fun.
Is it fun to count words?
Yes, it is fun!"""
import re
#from collections import Counter
def wordcount(text):
#text = text.strip()#去除空格
#这部分没有作用
# for i in text:
# if i=="," and i=="." and i=="?" and i=="!" and i==" ":#去掉标点符号
# i=""
word_list = re.findall(r'\b\w+\b', text.lower())
#word_list=[]
# 文章字符串前期处理:转换为小写字母,去掉空行,用空格分割生成一个单词列表
#word_list = text.replace('\n', '').lower().split(' ')
count_dict = {}#借助字典统计词频
# 如果字典里有该单词则加1,否则添加入字典
for word in word_list:
if word in count_dict.keys():
count_dict[word]+=1
else:
count_dict[word]=1
#没排序
print("排序前:",count_dict)
#按照词频从高到低排列
count_list=sorted(count_dict.items(),key=lambda x:x[1],reverse=True)
return count_list
print("排序后:",wordcount(text))
#key=lambda x:x[1] 其中X表示某一迭代出来的元素,其实是个元组,而x[1] 表示元组的第二个元素,即单词的次数(词频),若要按照单词排序则改成
#key=lambda x:x[0]
如果采取输入格式,在代码前的text改成 text=input("请输入要处理的文本:")
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.
"""
def wordcount(text):
pass
# #整理2
# text="""Hello world!
# This is an example.
# Word count is fun.
# Is it fun to count words?
# Yes, it is fun!"""
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.
"""
import re
#from collections import Counter
def wordcount(text):
#text = text.strip()#去除空格
#这部分没有作用
# for i in text:
# if i=="," and i=="." and i=="?" and i=="!" and i==" ":#去掉标点符号
# i=""
word_list = re.findall(r'\b\w+\b', text.lower())
#word_list=[]
# 文章字符串前期处理:转换为小写字母,去掉空行,用空格分割生成一个单词列表
#word_list = text.replace('\n', '').lower().split(' ')
count_dict = {}#借助字典统计词频
# 如果字典里有该单词则加1,否则添加入字典
for word in word_list:
if word in count_dict.keys():
count_dict[word]+=1
else:
count_dict[word]=1
#没排序
print("排序前:",count_dict)
#按照词频从高到低排列
count_list=sorted(count_dict.items(),key=lambda x:x[1],reverse=True)
return count_list
print("排序后:",wordcount(text))
#key=lambda x:x[1] 其中X表示某一迭代出来的元素,其实是个元组,而x[1] 表示元组的第二个元素,即单词的次数(词频),若要按照单词排序则改成
#key=lambda x:x[0]
请使用本地vscode连接远程开发机,将上面你写的wordcount函数在开发机上进行debug,体验debug的全流程,并完成一份debug笔记(需要截图)。
pip install debugpy安装一下,运行python -m debugpy --listen 5678 --wait-for-client ./myscript.py
将命令简化:
alias pyd='python -m debugpy --wait-for-client --listen 5678'
source ~/.bashrc
pyd ./myscript.py