0

i Have different words in two lists and also i have a commit words i need to get out the different words in two lists for Example list one:

apple
cut
chance

list two :

apple
cut
chance
help
there

my shot not working it give me repeated and there is unrepeated words my script

wcode = open('wcode.txt', 'r')
rcode = open('rcode.txt', 'r')
for new in wcode :
    if wcode.read() in rcode.read() :
        print(' Repeated ')
    else :
        print(' UnRepeated !!!' + '\n' + new)
Fathy
  • 377
  • 5
  • 13
  • 2
    https://stackoverflow.com/questions/18264471/in-python-how-do-i-find-common-words-from-two-lists-while-preserving-word-order – Mr_U4913 Jul 17 '17 at 21:20
  • 2
    Possible duplicate of [In Python, how do I find common words from two lists while preserving word order?](https://stackoverflow.com/questions/18264471/in-python-how-do-i-find-common-words-from-two-lists-while-preserving-word-order) – RyanZim Jul 18 '17 at 00:33

2 Answers2

0

It sounds like you want the symmetric difference between the two lists. For finding this, I would use Python's set operations.

After you've read the contents of yours files into lists, you can convert them to sets and compute the symmetric difference as shown below. At the end, you can convert the result back to a list if you want.

wcode = ['a', 'list', 'of', 'words', 'that', 'you', 'read', 'from', 'wcode', 'txt']
rcode = ['a', 'list', 'of', 'words', 'that', 'she', 'read', 'from', 'rcode', 'txt']
wcode = set(wcode)
rcode = set(rcode)

difference = wcode.symmetric_difference(rcode)

assert difference == {'she', 'you', 'wcode', 'rcode'}
final_result = list(difference)
0

Use sets:

s1 = {"apple", "cut", "chance"}
s2 = {"apple", "cut", "chance", "help", "there"}

print("Repeated: " + ", ".join(s1 & s2))
print("Not repeated: " + ", ".join(s1.symmetric_difference(s2)))

Edit

The bigger issue is that you should probably be consuming your files very differently:

wcode, rcode = (set(open(fn, "rb").read().splitlines()) for fn in [
    "rcode.txt",
    "wcode.txt"])
Ben
  • 2,422
  • 2
  • 16
  • 23
  • The big loss here is you don't preserve the order of the lists. Whether that's relevant is a question that must be asked of the OP. – TemporalWolf Jul 17 '17 at 21:28