Python: Compare 2 word lists


 
Thread Tools Search this Thread
Top Forums Programming Python: Compare 2 word lists
# 1  
Old 06-24-2011
Python: Compare 2 word lists

Hi.

I am trying to write a Python programme that compares two different text files which both contain a list of words. Each word has its own line

worda
wordb
wordc

I want to compare textfile 2 with textfile 1, and if there's a word in textfile 2 that is NOT in textfile 1, I want to print that word.

Here's what I've got so far (note that I'm an absolute beginner...)

Code:
def lookup(filename):
     f=file("textfile1")
     fileone=f.read
     g=file("textfile2")
     filetwo=g.read
     for line in filetwo:
     if line not in fileone:
          print line

Any suggestions are appreciated :-)

Thanks in advance,

Kat

---------- Post updated at 03:51 PM ---------- Previous update was at 02:57 PM ----------

I changed my code, andnow it actually does something...not what I want though:

Code:
fileone="./fileone.data"
filetwo="./filetwo.data"
words="./words.data"

with open(filetwo,'r') as a:
    with open(fileone,'r') as f:
        with open(words,'w') as w:
            for line in a:
                if line not in f:
                    w.write(line)

I get the same words in the created word list that I already have in filetwo...any suggestions?

Last edited by pludi; 06-24-2011 at 06:37 PM..
# 2  
Old 06-24-2011
Caveat: I have not programmed in python in quite a while.

As I recall, python supports a set data type and sets can be constructed from iterables (which include files).

Putting each list in a set, the answer you seek is the result of s2 - s1, where s1 is the set of lines (just words in this case) in textfile 1 and s2 corresponds to textfile 2.

This may not be a suitable approach if the lists are very very very very large (too large to be stored in memory), but that is seldom the case.

Regards,
Alister
# 3  
Old 06-24-2011
Look at PLEAC in Arrays in the section: Finding Elements in One Array but Not Another
# 4  
Old 06-24-2011
Quote:
Originally Posted by yazu
Look at PLEAC in Arrays in the section: Finding Elements in One Array but Not Another
From the section of the webpage that you recommended:
Code:
#    build lookup table
for item in b_list:
    seen[item] = 1

#    find only elements in a_list and not in b_list
for item in a_list:
    if not item not in seen:
        # it's not in 'seen', so add to 'aonly'
        aonly.append(item)

That code does the exact opposite of what it's supposed to do. One of the "not" boolean operators needs to go. For the sake of readability, preferably the first one.

Feel free to pass along a patch to the authors if you like.

Regards,
Alister
# 5  
Old 06-24-2011
Hey, it is under the comment:
# DON'T DO THIS.
# 6  
Old 06-25-2011
Quote:
Originally Posted by yazu
Hey, it is under the comment:
# DON'T DO THIS.
I don't think the "DON'T DO THIS" is intended to indicate that the code gives an erroneous result, but rather to indicate that it's not a very pythonic approach. The fact that the very next "DON'T DO THIS" section gives the correct result would seem to support my supposition.

I point this out mostly for the original poster. It's frustrating (especially for a newbie) to get hung up on a code sample only to find out later, after much facepalming, that it was the teaching material that was the problem.

Regards,
Alister

Last edited by alister; 06-25-2011 at 12:30 AM..
# 7  
Old 06-25-2011
In any case, these are examples of bad code.

But yes, I have to agree with you - this "bad" code is worse than it should be.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Python Concatenating 2 Lists

I am just trying to concatenate two lists together, but I am not sure what is wrong with my code, it won't run. Thank you for any help. #!/usr/bin/python # Takes two lists and returns a list that is the concatenation of both of # them. A = B = def list_concat( A, B): print(A) ... (2 Replies)
Discussion started by: totoro125
2 Replies

2. Programming

Populating Lists in Def using Python

Dipping around in python again and need to create a def that will populate a list(content) with the files that os.walk finds from within this directory and then I will re.search through each files looking for content. In learning Python, can someone point me in the right direction. This is what I... (3 Replies)
Discussion started by: metallica1973
3 Replies

3. Shell Programming and Scripting

Separate Text File into Two Lists Using Python

Hello, I have a pretty simple question, but I am new to Python and am trying to write a simple program. Put simply, I want to take a text file that looks like this: 11111 22222 33333 44444 55555 66666 77777 88888 and produce two lists, one containing the contents of the left column, one the... (0 Replies)
Discussion started by: Tyler_92
0 Replies

4. Homework & Coursework Questions

[Python] Compare 2 lists

Hello, I'm new to the python programming, and I have a question. I have to write a program that prints a receipt for a restaurant. The input is a list which looks like: product1 product3 product8 .... In the other input file there is a list which looks like: product1 coffee 5,00... (1 Reply)
Discussion started by: dagendy
1 Replies

5. Shell Programming and Scripting

compare two lists on two files

I have two files A and B listing ip addresses and all the ip addresses in B are in A, and A includes other ip addresses now I want to get the list of the ip addresses that are in A but not in B how to achieve this? thanks (1 Reply)
Discussion started by: esolvepolito
1 Replies

6. Shell Programming and Scripting

Compare two lists with perl

Hi everybody! I'm trying to delete some elements from a list with two elements on each row agreeing with the elements in another list. Pratically I want a perl script able to take each element of the second list (that is a single column list), compare it with both elements of each row from the... (3 Replies)
Discussion started by: gabrysfe
3 Replies

7. Shell Programming and Scripting

Bash script to compare two lists

Hi, I do little bash scripting so sorry for my ignorance. How do I compare if the two variable not match and if they do not match run a command. I was thinking a for loop but then I need another for loop for the 2nd list and I do not think that would work as in the real world there could... (2 Replies)
Discussion started by: GermanJulian
2 Replies

8. UNIX for Dummies Questions & Answers

Compare 2 lists using a full and/or partial match at beginning of line?

hello all, I wonder if anybody might be able to help with this. I have file 1 and file2. Both files may contain thousands of lines that have variable contents. file1 234GH 5234BTW 89er 678tfg 234 234YT tfg456 wert 78gt gh23444 (7 Replies)
Discussion started by: Garrred
7 Replies

9. UNIX for Dummies Questions & Answers

compare 2 very large lists of different length

I have two very large datasets (>100MB) in a simple vertical list format. They are of different size and with different order and formatting (e.g. whitespace and some other minor cruft that would thwart easy regex). Let's call them set1 and set2. I want to check set2 to see if it contains... (2 Replies)
Discussion started by: uiop44
2 Replies

10. Shell Programming and Scripting

Compare lists of files

If I had a list of numbers in two different files, what would be the fastest and easiest way to find out which numbers in list B are not in list A without reading each number in list B one at a time and using grep thousands of times against list A? I have two very long lists of numbers and the... (4 Replies)
Discussion started by: keelba
4 Replies
Login or Register to Ask a Question