0

I am trying to split 2 lists, compare them and make a new list without the succesfully compared items in 2 lists.

So lets say List_1.txt =

Failed = abc
Failed = hfi
Failed = kdi

and List_2.txt =

1:1:1 - jdsfjdf
2:2:2 - iidf
3:3:3 - abc
6:3:1 - hfi
8:2:1 - kdi
3:1:5 - dua
3:1:2 - dfh

I want to compare those lists and make a new_list2 without the list_1 entries.

what I tried was:

treinrit = open('List_1', 'r')
lijna = treinrit.readlines()
treinrit.close()

annuleer_treinrit = open('List_2', 'r')
lijnb = annuleer_treinrit.readline()
annuleer_treinrit.close()

lijsta = []
lijstb = []

for a in lijna:
    clean = a.split(' - ')
    print(lijsta)

for b in lijnb:
    lijstb.append(lijnb.split(": "))

I just cant get the list to split properly. I only need the last bit of each file to compare but I don't know how.

54m
  • 719
  • 2
  • 7
  • 18
  • Have you checked this page out on SO: http://stackoverflow.com/questions/546508/how-can-i-split-a-file-in-python Hope it helps –  Sep 23 '16 at 13:33
  • Can you show the desired end results? – Hoopdady Sep 23 '16 at 13:39
  • @Hoopdady list_2 without abc, hfi and kdi. – 54m Sep 23 '16 at 13:40
  • Without the entire line or just without those strings? Like for `3:3:3 - abc` would you remove the entire line, or just the `abc` so the line would read `3:3:3 - `? – Hoopdady Sep 23 '16 at 13:43

2 Answers2

1
with open('File1', 'r') as f1:
    f1_stored = []
    for line in f1:
        f1_stored.append(line.split('=')[1].strip())
    with open('File2', 'r') as f2;
        output = []
        for line in f2:
            if not any(failed in line for failed in f1_stored):
                output.append(line)

The do what you want with output

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
1

Something like this

bad_stuff = []
with open('List_1', 'r') as fn:
    for line in fn:
        bad_stuff.append(line.split('=')[1].strip())


with open('List_2', 'r') as fn:
    for line in fn:
        if line.split(':')[1].strip() not in bad_stuff:
            print(line)

The list bad_stuff will have all the elements from the first file after the = sign (like abc, hfi and kdi)

Then check the second file, and only print if the part after the : sign is not in the list bad_stuff

Gábor Erdős
  • 3,599
  • 4
  • 24
  • 56
  • Thank you this helped me out a lot. What does the [1].strip do exactly? – 54m Sep 23 '16 at 13:50
  • 1
    `strip()` removes the leading and tailing spaces and newlines. There are commands like `rstrip()` which removes the 'right' so tailing spaces and newlines (Theres also lstrip() as you can guess) . – Gábor Erdős Sep 23 '16 at 14:01
  • Ah tahnks a lot! And the [1]? Does it add a space or something? – 54m Sep 23 '16 at 15:04
  • 1
    `[1]` is an index. When you call `split()` on a string, it splits the string, and puts it into a `list` In our case the list will have two elements, and we only need the second one. As in most programming languages list indices are numbered from 0. So `list[0]` will give you the first element, `list[1]` will give you the second – Gábor Erdős Sep 23 '16 at 15:07