Find out if a given string comes AFTER another string in a .txt file

Question:

I want to write a code that goes through a .txt file line by line and looks for strings that are provided in two lists like:

list1 = ["s1", "s2", "s3"]
list2 = ["string1", "string2", "string3"]

If a value from list1 appears in the file after a value from list2 appears in the file, I want to remove the value from list1.

So for example. Look at the content of this .txt-File:

stuff
stuff
s1 stuff
s2
stuff string1 stuff
s1

-> Now I want to keep s2 (and s3 and all the other strings of that list) in my list and want s1 removed from that list as s1 appeared in the text after a string of list2.

I have written some code but I cannot get it to work as it does not filter anything.

list1 = ["s1", "s2", "s3"]
list2 = ["string1", "string2", "string3"]

with open('file.txt', 'r') as f:
    for line in f.readlines():
        for table1 in list1:
            if table1 in line:
                for table2 in list2:
                    if table2 in line:
                        if table1 in list1:
                            list1.remove(table1)

print(list1)
Asked By: Jessy

||

Answers:

first you need to check the string belong to which list

use two object to save details, which list string belong and set them true

if string from list2 appear then set string_from_list2 to True and once this object is set true, then if any string from list1 appear then don’t append them to solution.otherwise append other string to solution array result

file = "file.txt"
result = []



list1 = ["s1", "s2", "s3"]
list2 = set(["string1", "string2", "string3"])
string_from_list2 = False

to_remove = set()

with open(file, 'r') as f:
        for string in f.readlines():
                string = string.strip("n")
                if string in list2:
                        string_from_list2 = True
                elif string in list1 and string_from_list2:
                        to_remove.add(string)
        list1 = list(filter(lambda x: x not in to_remove, list1))
print(list1)
Answered By: sahasrara62

If the file only contains lines with one word as the example, you could use this:

list1 = ["s1", "s2", "s3"]
list2 = ["string1", "string2", "string3"]

with open('file.txt', 'r') as f:
  last = False
  for line in f.readlines():
    line = line.strip()
    if last and (line in list1):
      list1.remove(line)
    last = line in list2
  print(list1)

output:
[‘s2’, ‘s3’]

Answered By: palvarez

file.txt:

stuff
stuff
s1
s2
string1
s1
list1 = {"s1", "s2", "s3"}
list2 = {"string1", "string2", "string3"}
remove_set = set() # to keep track of which strings from list1 need to be removed.
seen2 = False 

with open('file.txt', 'r') as f:
    for line in f:
        if seen2:
            for table1 in list1:
                if table1 in line:
                    remove_set.add(table1)
        else:
            for table2 in list2:
                if table2 in line:
                    seen2 = True
                    break

list1 -= remove_set
print(list(list1))

Output:

['s2', 's3']
Answered By: Maria Afara
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.