我正在嘗試創建一個嵌套字典,它告訴我每個單詞出現在哪個檔案中以及它出現在哪個位置:例如:
dictionary ={}
textfile_list = ['file1.txt', 'file2.txt', 'file3.txt']
file_contents = ['mario luigi friend mushroom', 'rick mario morty portal summer mario', 'peter griffin shop']
#first element corresponds to the contents of file1.txt and etc.
words = [['mario', 'luigi', 'friend', 'mushroom'],
['rick', 'mario', 'morty', 'portal', 'summer', 'mario'],
['peter', 'griffin', 'shop']] #tokenising the text
我想要 print(dictionary['mario']) 給 [{'file1.txt': [0]}, {'file2.txt': [1,5]} ]
到目前為止我的代碼是:
dict = {}
for i in range(len(textfile_list)):
check = file_contents
for item in words: #a list of every word from every file ['word1','wordn','word3',...]
if item in check:
if item not in dict:
dict[item] = []
if item in dict:
dict[item].append(textfile_list[i])
dict = {k: list(set(v)) for k, v in dict.items()}
我不知道如何在我目前沒有的嵌套字典中實作單詞的位置!有人可以幫忙嗎?
uj5u.com熱心網友回復:
你的一層嵌套太多了。第一個描述對應于一個詞典,其鍵字,并且其值是字典的(檔案名,position_list)對(例如dictionary['mario'] = {'file1.txt': [0], 'file2.txt': [1, 5]}),而不是一個詞典,其鍵字,和其值是一個與每個字典一個檔案名詞典的串列,就像你一樣。
textfile_list = ['file1.txt', 'file2.txt', 'file3.txt']
file_contents = ['mario luigi friend mushroom', 'rick mario morty portal summer mario',
'peter griffin shop']
# first element corresponds to the contents of file1.txt and etc.
# words = [string_list.split() for string_list in file_contents]
words = [['mario', 'luigi', 'friend', 'mushroom'],
['rick', 'mario', 'morty', 'portal', 'summer', 'mario'],
['peter', 'griffin', 'shop']] # tokenising the text
dictionary = {}
for textfile_name, file_strings in zip(textfile_list, words):
for position, word in enumerate(file_strings):
if word not in dictionary:
dictionary[word] = {}
if textfile_name not in dictionary[word]:
dictionary[word][textfile_name] = []
dictionary[word][textfile_name].append(position)
print(dictionary['mario'])
>>> {'file1.txt': [0], 'file2.txt': [1, 5]}
我不確定最后一行是什么,因為目前沒有重復;無論如何,不??要dict在 Python 中用作變數名,因為它是內置的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/339424.html
下一篇:如何為串列指定索引名稱?
