2017-10-07 106 views
-2

好吧,我有一所學校分配,我NEET兩個文件相互比較。這很簡單,程序需要展現的東西像所有的在這兩個文件中,例如獨特字;比較兩個文件與Python

file1的: 這是一個測試

file2的: 這不是測試

輸出: [ 「這」, 「是」, 「一」, 「測試」, 「不」 ]

這就是我從這個一小段代碼預期輸出:

def unique_words(file_1, file_2): 
    unique_words_list = [] 
    for word in file_1: 
     unique_words_list.append(word) 
    for word in file_2: 
     if word not in file_1: 
      unique_words_list.append(word) 
    return unique_words_list 

但這並沒有發生,不幸的是,這是輸出:

['this \ n','是\ n','a \ n','test','this \ n','是\ n','not \ n','a \ N」,‘測試’]

我有多個函數,幾乎相同的方式工作,也有類似的輸出。我知道爲什麼\ n出現,但我不知道如何擺脫它。 如果有人可以幫助我得到這個正確的輸出,這將是一個很大的幫助:)

+0

對不起,但該任務明確告訴我使用列表:我 – GotYa

+0

該實際上,工作。有一個/ n,因爲該文件是在單獨的一行中設置的每個單詞,因爲我只知道如何循環遍線。 – GotYa

+0

您能向我解釋爲什麼比較這些文件不起作用嗎? – GotYa

回答

1

來自Steampunkery的解決方案是不正確的:(1)它不處理每行大於1個字的文件,(2)它沒有考慮file1.txt中的重複單詞(嘗試使用file1行「單詞單詞單詞」 - 應得到一個「單詞」輸出,但你得到四個)。此外for/if構造是不需要的。

這裏是一個緊湊的,正確的解決方案。

FILE1.TXT的內容:文件2的

the cat and the dog 
the lime and the lemon 

內容。TXT:

the mouse and the bunny 
dogs really like meat 

代碼:

def unique(infiles): 
    words = set() 
    for infile in infiles: 
     words.update(set([y for x in [l.strip().split() for l in open(infile, 'r').readlines()] for y in x])) 
    return words 

print unique(['file1.txt']) 
print unique(['file2.txt']) 
print unique(['file1.txt', 'file2.txt',]) 

輸出:

set(['and', 'lemon', 'the', 'lime', 'dog', 'cat']) 
set(['and', 'like', 'bunny', 'the', 'really', 'mouse', 'dogs', 'meat']) 
set(['and', 'lemon', 'like', 'mouse', 'dog', 'cat', 'bunny', 'the', 'really', 'meat', 'dogs', 'lime']) 

兩個教訓Python的學習:

  1. 使用工具的語言給你,像set
  2. 考慮輸入條件,打破你的算法
+0

哦,哇,你說得對,我沒有注意到。我將工作你發送到我自己的代碼,謝謝你! – GotYa

0

這裏是一個小片段我寫重用你的一些代碼:

#!/usr/bin/env python3.6 

with open('file1.txt', 'r') as file1, open('file2.txt', 'r') as file2: 
    file_1 = file1.readlines() 
    file_1 = [line.rstrip() for line in file_1] 
    file_2 = file2.readlines() 
    file_2 = [line.rstrip() for line in file_2] 


def unique_words(file_1, file_2): 
    unique_words_list = file_1 
    for word in file_2: 
     if word not in unique_words_list: 
      unique_words_list.append(word) 
    return unique_words_list 


print(unique_words(file_1, file_2)) 

此腳本假定你有2檔名爲file1.txtfile2.txt,分別在同一目錄下的腳本。從你的例子中,我們也假定每個單詞都在它自己的行上。下面是通過散步:

  1. 打開這兩個文件,讀他們的行成一個列表,列表理解
  2. 定義一個函數,增加了第一個文件中的所有單詞的列表,然後刪除換行符將所有的話不在第二個文件是名單列表
  3. 打印使用我們的文件,我們作爲輸入前面讀該函數的輸出。
+0

啊啊謝謝:) 我想我可以用這個小片段做到這一點! – GotYa

+0

你爲什麼不接受答案? – Steampunkery

+0

CHEck其他答案 – GotYa