【发布时间】:2021-05-09 16:57:44
【问题描述】:
我已经阅读了很多帖子,但没有运气。
到目前为止,我已经尝试过.split() 和regex。
注意:我在 repl.it/ 上运行此代码。
import math
documents = [
["It is going to rain today"],
["Today I am not going outside"],
["I am going to watch the season premiere"]
]
docs = 1000
words_per_doc = 100 # length of doc
dp = 4
# -- Setup --
all_words = [] # all instances
for doc in documents:
for s in doc:
words = s.split()
print(words)
all_words.append(words)
all_words = sorted(all_words) # alphabeticalise
all_words = list(dict.fromkeys(all_words)) # remove duplicates
print('All Words')
print(all_words)
print()
print('Binary Scoring')
for doc in documents:
scoring = []
for word in all_words:
if word in doc:
scoring.append(1)
else:
scoring.append(0)
print("\"" + doc + "\" = " + scoring)
print()
错误:
['It', 'is', 'going', 'to', 'rain', 'today']
['Today', 'I', 'am', 'not', 'going', 'outside']
['I', 'am', 'going', 'to', 'watch', 'the', 'season', 'premiere']
Traceback (most recent call last):
File "main.py", line 6, in <module>
import BagofWords
File "/home/runner/DeepLearning/BagofWords.py", line 21, in <module>
all_words = list(dict.fromkeys(all_words)) # remove duplicates
TypeError: unhashable type: 'list'
【问题讨论】:
-
你没有一个字符串列表,你有一个列表列表。这有什么原因吗?子列表中可能包含多个字符串吗?
-
请不要在问题中编辑解决方案公告。接受(即单击旁边的“勾选”)现有答案之一,如果有的话。如果现有答案尚未涵盖您的解决方案,您还可以创建自己的答案,甚至接受它。
标签: python regex string list split