2013-04-11 129 views
2

我有一個邏輯錯誤的排序,我似乎無法挑出它。以下是我有:Python:列表附加問題

Document = 'Sample1' 
locationslist = [] 
thedictionary = [] 
userword = ['the', 'a'] 
filename = 'Sample1' 
for inneritem in userword: 
    thedictionary.append((inneritem,locationslist)) 
    for position, item in enumerate(file_contents): 
     if item == inneritem: 
      locationslist.append(position) 
wordlist = (thedictionary, Document) 
print wordlist 

所以基本上我試圖創建一個較小的列表(locationslist)與特定userword一起較大的列表(thedictionary)。我幾乎擁有它,除了我的輸出是將所有單詞的所有位置(其中只有2個 - 'the''a')放在每個列表中。似乎有一個簡單的邏輯問題 - 但我似乎無法發現它。輸出是:

([('the', [5, 28, 41, 97, 107, 113, 120, 138, 141, 161, 2, 49, 57, 131, 167, 189, 194, 207, 215, 224]), 
    ('a', [5, 28, 41, 97, 107, 113, 120, 138, 141, 161, 2, 49, 57, 131, 167, 189, 194, 207, 215, 224])], 
'Sample1') 

但應該是:

([('the', [5, 28, 41, 97, 107, 113, 120, 138, 141, 161]), 
    ('a', [2, 49, 57, 131, 167, 189, 194, 207, 215, 224])], 
'Sample1') 

查看如何既位置列表被附加到每個都與每個userwords 'the''a'的問題的輸出?我可以在這裏使用關於我在做什麼的建議..

回答

3

你只創建一個locationslist,所以你只有一個。它由兩個詞共享。您需要創建在每次循環迭代新locationslist

for inneritem in userword: 
    locationslist = [] 
    thedictionary.append((inneritem,locationslist)) 
    # etc. 
+0

謝謝BrenBarn ..有時候是那些看不到的小修補,只是讓你想給自己一個打樁機... – Relative0 2013-04-11 20:02:57

1

你只是創建了一個locationslist,所以所有的locationslist.append()通話修改名單。您可以將locationslist附加到thedictionary中的元組中,因爲您有userword中的元素。您應該爲userword的每個元素創建一個位置列表。

你可以寫成一組嵌套列表內涵的算法,這將導致正確的列表創建:

user_word = ['the', 'a'] 
word_list = ([(uw, 
       [position for position, item in enumerate(file_contents) 
       if item == uw]) 
       for uw in user_word], 
      'Sample1') 

那會還叫enumerate(file_contents)一次爲每個項目在user_word,這可能如果file_contents很大,則價格昂貴。

我建議你重寫這個傳遞過來file_contents一次,在反對user_word內容的每個位置檢查項目,位置追加到僅名單在那個位置上發現的特定user_word。我會建議使用字典,以保持user_word列表分離和訪問:

document = 'Sample1' 

temp_dict = dict((uw, []) for uw in user_word) 

for position, item in enumerate(file_contents): 

if item in temp_dict: 
    temp_dict[item].append(position) 

wordlist = ([(uw, temp_dict[uw]) for uw in user_word], document) 

兩種解決方案將讓你每個user_word的位置,出場順序,在文件被掃描。它也將返回你正在尋找的列表結構。

+0

謝謝pcurry。 – Relative0 2013-04-11 21:33:00