Reputation: 23
im not that good in programming, just doing it for fun and getting code samples online. so i decided i wanted to do the following:
However, i keep getting 'unhashable type: 'list'. I assume that this is because I use frequency_list but I don't understand how do i go about that.
Error traceback: Traceback (most recent call last): File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\whereby.py", line 31, in print(int(frequency[words])/wordCount) TypeError: unhashable type: 'list'
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)
Upvotes: 0
Views: 760
Reputation: 2150
In your words = p.split()
line you are setting words
to be a list
object from splitting.
Your error is saying you cannot get the item at the index of words
in frequency
in your last line since list
s cannot be hashed. You can read more about how Python dictionaries work and why hashing is a thing they use here.
I think you mean to put something else instead of words
in your last line, maybe 'whereby'
.
Upvotes: 1