1

I need to compare two lists in a program to see if there are matching strings. One of them is a txt document that I already imported. Thats what I did

    def compareLists(self, listA, listB):
    sameWords = list()

    for a in xrange(0,len(listA)):
        for b in xrange(0,len(listB)):
            if listA[a] == listB[b]:
                sameWords.append(listA[a])
                pass
            pass
        pass
    return sameWords

But if I run the program it doesnt show any matches although I know that there has to be one. I think its somewhere inside the if block.

Lucas Mähn
  • 736
  • 2
  • 6
  • 19
  • have you tried out a debugger? You can easily observe, which values listA or listB have in each iteration step. – Rockbar Oct 09 '16 at 17:38
  • You are looking at a set intersection operation it seems. – Reut Sharabani Oct 09 '16 at 17:39
  • 5
    `list(set(listA) & set(listB))` will return exactly what you want, as shown [here](http://stackoverflow.com/questions/642763/python-intersection-of-two-lists). – Efferalgan Oct 09 '16 at 17:39
  • 2
    remove the `pass`. It's useless here. – Daniel Oct 09 '16 at 17:41
  • 2
    I'm surprised actually that this doesn't show matches. Even though it is inefficient and the `pass` is unnecessary, it should still give you a list of all the matches found (with duplicates included). – Jack Ryan Oct 09 '16 at 17:43
  • it's working fine for input : `listA=['hiii','kkkk'] listB=['jiii','ggg','hiii']` it gives : `['hiii']` as output. I tried in Python online compiler. – Kalpesh Dusane Oct 09 '16 at 17:44
  • It's working too if I dont use a list that imported a .txt file but with the .txt its not working. – Lucas Mähn Oct 09 '16 at 17:48
  • 1
    Do a `print()` of the list made from the imported file. My guess is that the words it contains are ending with `\n`. – Efferalgan Oct 09 '16 at 17:50

1 Answers1

1

I am assuming the indentation is correct in your code. Continuing with your strategy, this code should work.

def compareLists(self, listA, listB):
    sameWords = list()

    for a in xrange(0,len(listA)):
        for b in xrange(0,len(listB)):
            if listA[a] == listB[b]:
                sameWords.append(listA[a])
    return sameWords

Alternatively, as @Efferalgan suggested, simply do the set intersection.

def compareLists(self, listA, listB):
    return list(set(listA) & set(listB))

Note: The set intersection will remove duplicate matching words from your result.

As you said, you are reading in the lines from a text file, and it looks like the newlines are still in there.

my_text_list = [s for s in open("my_text.txt").read().rsplit()]
Jack Ryan
  • 1,287
  • 12
  • 26
  • It's still not working. Im pretty sure its because of the .txt file but i can print it out as a list so I imported it right. Could it be possible that the file is just too big? It has about 15,000 lines – Lucas Mähn Oct 09 '16 at 17:52
  • The size of the list should not matter, it is not *that* big. Print the list, and visually check that the words in it are what you expect. – Efferalgan Oct 09 '16 at 17:55
  • 15,000 lines should be fine. Could you post some of the list that you printed? – Jack Ryan Oct 09 '16 at 17:55
  • 'Weiltingen\n', 'Unterschwaningen\n', 'Theilenhofen\n', 'R\xc3\xb6ckingen\n', 'Pfofeld\n', 'Ornbau\n', 'Muhr am See\n', – Lucas Mähn Oct 09 '16 at 17:59
  • After I removed the \n s its showing this: > (Its scrapy btw) – Lucas Mähn Oct 09 '16 at 18:00
  • Call `.rsplit()` on the strings to remove the newlines when you read in the file. – Jack Ryan Oct 09 '16 at 18:02
  • Okay I got it. thank you very much guys and sorry if this question might be a little bit stupid, Im a little bit new to that. – Lucas Mähn Oct 09 '16 at 18:13