比较两个文件并在Python中找到匹配的单词 [英] compare two file and find matching words in python

查看:473
本文介绍了比较两个文件并在Python中找到匹配的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件:第一个包括条款和他们的频率:

pre code表2
苹果4
铅笔89

第二个文件是字典:

 国外
苹果
面包
...

我想检查第一个文件是否包含第二个文件中的任何单词。例如,第一个文件和第二个文件都包含apple。
我是python的新手。
我尝试了一些东西,但是不起作用。你可以帮帮我吗 ?谢谢

  for line in dictionary:
words = line.split()
print words [0 ]

for line2 in test:
words2 = line2.split()
print words2 [0]

解决方案

类似于这样:

 打开(file1)为f1,打开(file2)为f2:
words = set(line.strip()为f1中的行)#从字典文件$ b $创建一组单词b
#为什么设置?集合提供了一个O(1)查找,所以整体的复杂度是O(N)

#now循环其他文件(word,freq文件)的每一行
在f2中的行:
单词,freq = line.split()#fetch单词,freq
如果单词在单词中:#if单词在单词集中找到然后打印
打印单词

输出:

  apple 


I have a two file: the first one includes terms and their frequency:

table 2
apple 4
pencil 89

The second file is a dictionary:

abroad
apple
bread
...

I want to check whether the first file contains any words from the second file. For example both the first file and the second file contains "apple". I am new to python. I try something but it does not work. Could you help me ? Thank you

for line in dictionary:
    words = line.split()
    print words[0]

for line2 in test:
    words2 = line2.split()
    print words2[0]

解决方案

Something like this:

with open("file1") as f1,open("file2") as f2:
    words=set(line.strip() for line in f1)   #create a set of words from dictionary file

    #why sets? sets provide an O(1) lookup, so overall complexity is O(N)

    #now loop over each line of other file (word, freq file)
    for line in f2:
        word,freq=line.split()   #fetch word,freq 
        if word in words:        #if word is found in words set then print it
            print word

output:

apple

这篇关于比较两个文件并在Python中找到匹配的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆