words=[] wordlist = [] def get(): words=[] text = entry2.get() words.append(text) wordlist = [] f = open('D:\Python文档保存\红楼梦.txt', 'r', encoding='utf-8') text = f.read() plt.rcParams['font.sans-serif'] = 'SimHei' sWords = jieba.lcut(text) for word in sWords: if word in words: wordlist.append(word) collections.Counter(wordlist) def show_result(): word_count=collections.Counter(wordlist) result_str="Word count:{}".format(word_count) messagebox.showinfo("输出结果", result_str) 代码如上,怎么解决输出内容{}里还是count()的问题
时间: 2023-06-07 11:07:34 浏览: 66
您好,根据您提供的代码,输出内容为"Word count:{}",但是{}没有被替换成实际的字数统计结果。要解决这个问题,需要将字数统计结果传递给format()进行格式化输出。可以改为如下代码:
```
def show_result():
word_count = collections.Counter(wordlist)
result_str = "Word count:{}".format(word_count)
messagebox.showinfo("输出结果", result_str.format(len(wordlist)))
```
在使用messagebox输出结果时,调用format()将len(wordlist)传递进去,这样就可以替换{}了。
相关问题
def get_content(html_url): response = get_response(html_url=html_url) selector = parsel.Selector(response.text) title = selector.css('.grap--h2 ::text').get() content_list = selector.css('.grap ::text').getall() content = ''.join(content_list) return title, content def main(): url = f'https://hongloumeng.5000yan.com/' name, link_list = get_novel_info(novel_url=url) for link in link_list: title, content = get_content(html_url=link) save(name, title, content) word=[] wordlist = [] #这里是定义函数加进列表里,想让entry的内容作为变量参与函数的运行 def add_to_list(): text = entry2.get() words.append(text) wordlist = [] for word in sWords: if word in words: wordlist.append(word) def function(): f=open('D:\Python文档保存\红楼梦.txt','r',encoding='utf-8') text = f.read() plt.rcParams['font.sans-serif'] = 'SimHei' sWords = jieba.lcut(text) #wordlist = [] #words = ['刘姥姥', '贾', '王夫人'] #for word in sWords: # if word in words: # wordlist.append(word) word_counts = collections.Counter(wordlist) print(wordlist) print(word_counts) #按钮3 button3=tk.Button(root,text='词频统计图',bg="#FFB6C1",command=func()) button3.place(x=320,y=165) 把前面的代码绑定在这个按钮上,改一下代码
这是一段 Python 代码,包括两个函数和两个列表。
get_content(html_url) 函数的作用是从给定的 html_url 中获取网页内容(包括标题和正文),并返回标题和正文的字符串形式。
main() 函数利用 get_novel_info() 函数获取小说信息(小说名和链接列表),然后遍历链接列表,调用 get_content() 函数获取每个链接对应的内容,并将小说名、章节名、正文内容一起保存。
word 和 wordlist 两个变量在这段代码中没有用到,可能是之后代码需要用到的变量。
wordlist2 = jieba.cut(sent)得出的wordlist2 是什么类型的数据
?你的第一个问题是:wordlist2 = jieba.cut(sent)得出的wordlist2 是什么类型的数据?
回答:wordlist2 是一个可迭代的生成器类型的数据,其中包含了分词后得到的所有单词。