Search logic and exclude multiple matches from the list

I need to match the contents of the list with the given template and form another list that will contain everything except matches. Meaning, I'm trying to make a list of exceptions.

Now with one pattern matching, it's easy. But for more than that, it becomes complicated.

Let's look at an example:

Lmain=[arc123, arc234,xyz111,xyz222,ppp999,ppp888]

for count in range(len[Lmain]):

    if Pattern matches Lmain[i]:
              Pass
    else:result.append(Lmain[i])

Now let's say pattern = arc, my result will be

result = [xyz111,xyz222,ppp999,ppp888]

This is just the logic where I will use the regex to match.

Now, if we have 2 patterns, then using the above logic in a loop:

Pattern=['arc','xyz']

for pat in Pattern:
      if pat matches Lmain[i]:
          Pass
      else:result.append(Lmain[i])

This will give us the wrong result.

result = [xyz111,xyz222,ppp999,ppp888,arc123,arc234,ppp999,ppp888]

So you can see that the logic just doesn't work.

My plan:

First, we find the exception list for the first template, which will give us the result:

result = [xyz111,xyz222,ppp999,ppp888]

For the second template, we need to look at the result above.

if Pattern matches Result[i]:
      Pass
else:result_final.append(Result[i])

, Recursion . , ? , . .

- , , , .

+3
3

:

>>> import re
>>> Lmain=['arc123', 'arc234', 'xyz111', 'xyz222','ppp999','ppp888']
>>> Pattern=['arc','xyz']
>>> [x for x in Lmain if not any(re.search(y, x) for y in Pattern)]
['ppp999', 'ppp888']
+5
for item in lst:
    if all(pat not in item for pat in patterns):
        exclude_list.append(item)

in , (, item.startswith(pat))

, , , :

matches = [x for x in lst if any(x.startswith(p) for p in patterns)]
exclude_list = list(set(lst).difference(matches))

(, , ) - ( filter):

import re
expr = '^(?!%s)' % '|'.join(patterns)
exclude_list = filter(re.compile(expr).search, lst)
+4
matched = False
for pat in Pattern:
    if pat patches Lmain[i]:
        matched = True
        break;
if matched:
    Pass
else:
    result.append(Lmain[i])
+1

All Articles