2017-10-07 78 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