compare two file and find matching words in python -


i have 2 file: first 1 includes terms , frequency:

table 2 apple 4 pencil 89 

the second file dictionary:

abroad apple bread ... 

i want check whether first file contains words second file. example both first file , second file contains "apple". new python. try not work. me ? thank you

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

something this:

with open("file1") f1,open("file2") f2:     words=set(line.strip() line in f1)   #create set of words dictionary file      #why sets? sets provide o(1) lookup, overall complexity o(n)      #now loop on each line of other file (word, freq file)     line in f2:         word,freq=line.split()   #fetch word,freq          if word in words:        #if word found in words set print             print word 

output:

apple 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -