Home > Blockchain >  Is there a way to compare each list item to another list of exeptions?
Is there a way to compare each list item to another list of exeptions?

Time:07-29

I have two lists with strings. I need to compare each item in the first list to list of exeptions in the other list and replace items in the first list with ' ' if there is the same item in two lists. List of exeptions is in the .txt document. I've tried to make some code but it seems to work only with string list written in actual vs code.

with open('input.txt') as f:
    a = f.read().splitlines()
with open('exeptions.txt') as f:
    c = f.read().splitlines()
if c != []:
    for i in range(len(a)):
        if a[i] == 'exeption':
            a[i] = ' '

Example of txt files:

  • input.txt - ['a', 'b']
  • exeptions.txt - ['b']
  • end goal - ['a', ' ']

CodePudding user response:

for i,thing in enumerate(a):
    if thing in b:
        a[i] = ''

If the empty string placeholder is not necessary then you could use a set

a = list(set(a).difference(b))
  • Related