0

I am trying to create a script that will

  • look at each word in a text document and store in a list (WordList)
  • Look at a second text document and store each word in a list (RandomText)
  • Print out the words that appear in both lists

I have come up with the below which stores the text in a file, however I can't seem to get the function to compare the lists and print similarities.

KeyWords = open("wordlist.txt").readlines() # Opens WordList text file, Reads each line and stores in a list named "KeyWords". 
RanText = open("RandomText.txt").readlines() # Opens RandomText text file, reads each line and stores in a list named "RanText"


def Check():
    for x in KeyWords:
        if x in RanText:
            print(x)

print(KeyWords)
print(RanText)
print(Check)

Output:

C:\Scripts>python Search.py
['Word1\n', 'Word2\n', 'Word3\n', 'Word4\n', 'Word5']
['Lorem ipsum dolor sit amet, Word1 consectetur adipiscing elit. Nunc fringilla arcu congue metus aliquam mollis.\n', 'Mauris nec maximus purus. Maecenas sit amet pretium tellus. Praesent Word3 sed rhoncus eo. Duis id commodo orci.\n', 'Quisque at dignissim lacus.']
<function Check at 0x00A9B618>
Keith Gaughan
  • 21,367
  • 3
  • 32
  • 30
LockTheTaskBar
  • 99
  • 4
  • 13

2 Answers2

1
print(Check)

will only print object, just invoke function

check()

print statement inside this function will work and print what you write. For test purposes just create two list, if comparing function will work, then try it on lists from files.

or just use solution from compare two list

darvark
  • 314
  • 3
  • 15
  • Thanks, that makes sense. Just tested and no longer getting "". However the script is not printing "Word1" which appears in both texts. I think "KeyWords" list is storing this as "Word1\n" rather than "Word1". – LockTheTaskBar Jun 21 '18 at 11:34
  • is't a good practice to strip all string elements in list from non printable marks. unless you need them, but mostly you don't) – darvark Jun 21 '18 at 11:47
1

Instead of print(Check) you should just call Check() since it prints and does not return anyting.

Another option would be:

KeyWords = []
with open("wordlist.txt") as f:
    for line in f:
        KeyWords.append(line.strip())

RanText = []
with open("RandomText.txt") as f:
    for line in f:
        RanText.append(line.strip())

in_common = [word for word in KeyWords if word in RanText]
print(in_common)
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
  • Thanks, that makes sense. Just tested and no longer getting "". However the script is not printing "Word1" which appears in both texts. I think "KeyWords" list is storing this as "Word1\n" rather than "Word1" – LockTheTaskBar Jun 21 '18 at 11:55
  • @LockTheTaskBar I have edited my answer to solve that as well. It's a bit longer code, but it is the correct way of reading data from a file. – alec_djinn Jun 21 '18 at 11:57