Я уверен, что решение моей проблемы довольно простое, но я не смог его найти.
При запуске я получаю
TypeError: unhashable введите: 'list'
, что ожидается, так как я работаю со списком:
import operator
import shutil
def start(source):
source=open('Book.txt', 'r', encoding='UTF-8')
wordslist=[]
for word in source:
content=word.lower().split()
for each_word in content:
#print(each_word)
wordslist.append(content)
#cleanuptext(wordslist)
sortdictionnary(wordslist)
'''
def cleanuptext(wordslist):
cleanwords=[]
for cleanword in wordslist:
symbols=',.'
for i in range(0, len(symbols)):
cleanword = str(cleanword).replace(symbols[i], "")
if len(cleanword) > 0:
print(cleanword)
cleanwords.append(cleanword)
'''
def sortdictionnary(wordslist):
counter={}
for word in wordslist:
if word in counter:
counter[word] += 1
else:
counter[word] = 1
for key, value in sorted(counter.items(), key=operator.itemgetter(0)):
print(key, value)
start(source='Book.txt')
Как я могу избежать этой проблемы и иметь возможность использовать свой список в моем словарь?
Даже если я пытался использовать счетчик, у меня возникла та же проблема:
import operator
import shutil
from collections import Counter
def start(source):
source=open('Book.txt', 'r', encoding='UTF-8')
wordslist=[]
for word in source:
content=word.lower().split()
for each_word in content:
#print(each_word)
wordslist.append(content)
#cleanuptext(wordslist)
sortdictionnary(wordslist)
'''
def cleanuptext(wordslist):
cleanwords=[]
for cleanword in wordslist:
symbols=',.'
for i in range(0, len(symbols)):
cleanword = str(cleanword).replace(symbols[i], "")
if len(cleanword) > 0:
print(cleanword)
cleanwords.append(cleanword)
'''
def sortdictionnary(wordslist):
coun = Counter()
for word in wordslist:
if word in coun:
coun[word] += 1
else:
coun[word] = 1
for key, value in sorted(coun.items(), key=operator.itemgetter(0)):
print(key, value)
start(source='Book.txt')
Спасибо и всего наилучшего