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.

8
  • have you tried out a debugger? You can easily observe, which values listA or listB have in each iteration step. Commented Oct 9, 2016 at 17:38
  • 5
    list(set(listA) & set(listB)) will return exactly what you want, as shown here. Commented Oct 9, 2016 at 17:39
  • 2
    remove the pass. It's useless here. Commented Oct 9, 2016 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). Commented Oct 9, 2016 at 17:43
  • 1
    Do a print() of the list made from the imported file. My guess is that the words it contains are ending with \n. Commented Oct 9, 2016 at 17:50

1 Answer 1

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()]
Sign up to request clarification or add additional context in comments.

7 Comments

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
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.
15,000 lines should be fine. Could you post some of the list that you printed?
'Weiltingen\n', 'Unterschwaningen\n', 'Theilenhofen\n', 'R\xc3\xb6ckingen\n', 'Pfofeld\n', 'Ornbau\n', 'Muhr am See\n',
After I removed the \n s its showing this: <bound method AlinocrawlerSpider.compareLists of <AlinocrawlerSpider 'alinocrawler' at 0x5272780>> (Its scrapy btw)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.