Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python不断返回typeerror:Unhabable type:list_Python - Fatal编程技术网

python不断返回typeerror:Unhabable type:list

python不断返回typeerror:Unhabable type:list,python,Python,我不是很擅长编程,只是为了好玩和在线获取代码示例。所以我决定做以下几件事: 计算总字数 数一数单词 除以“where”计算总字数 然而,我不断得到“不可损坏类型:'列表'”。我想这是因为我使用了频率列表,但我不知道该怎么做 错误回溯: 回溯最近一次呼叫上次: 文件C:\Users\user\AppData\Local\Programs\Python36-32\Python.py,第31行,在 printintfrequency[字数]/字数 TypeError:不可损坏的类型:“列表” 在您的w

我不是很擅长编程,只是为了好玩和在线获取代码示例。所以我决定做以下几件事:

计算总字数 数一数单词 除以“where”计算总字数 然而,我不断得到“不可损坏类型:'列表'”。我想这是因为我使用了频率列表,但我不知道该怎么做

错误回溯:
回溯最近一次呼叫上次: 文件C:\Users\user\AppData\Local\Programs\Python36-32\Python.py,第31行,在 printintfrequency[字数]/字数 TypeError:不可损坏的类型:“列表”

在您的words=p.split行中,您正在将words设置为一个列表对象,以防止拆分

您的错误是无法在最后一行的单词频率索引中获取项目,因为列表无法散列。您可以阅读更多关于Python词典如何工作以及为什么使用哈希的内容


我想你的意思是在你的最后一行写点别的而不是文字,也许是“whiche”

我们可以查看错误跟踪吗?回溯上次调用:文件C:\Users\user\AppData\Local\Programs\Python\Python36-32\where.py,第31行,在printintfrequency[words]/wordCount TypeError:Unhable type:“list'I suspect'words”是一个列表,而不是一个关键字-不能将列表用作dict关键字。检查调试器中的“words”,并验证它是否符合您的需要。
import re
import string

frequency = {}
wherebyity = {}
document_text = open('C:/Users/user/desktop/text.txt', 'r')
text_string = document_text.read().lower()
match_pattern = re.findall('whereby', text_string)

for word in match_pattern:
    count = frequency.get(word, 0)
    frequency[word] = count + 1

frequency_list = frequency.keys()

for words in frequency_list:
    print (words, frequency[words])
    print (type(frequency))
    print (type(frequency[words]))
    print (frequency)

with open('C:/Users/user/desktop/text.txt', 'r') as f:
    p = f.read() # p contains contents of entire file
    # logic to compute word counts follows here...
    words = p.split()
    wordCount = len(words)
    print ("The total word count is:", wordCount)
    print (type(wordCount))


    print(int(frequency[words])/wordCount)