简体   繁体   中英

How to exclude one word from python list?

I am trying to work on Python Regex. I have a list as follows:

['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-2', 'MYTAB-PERF','ABC','DEF']

I need to select all values start with MYTAB , excluding MYTAB-2 . Result should be :

['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-PERF']

What should be the regex syntax for it?

You can use this piece of code.

  1. It will loop through the initial list l1 and if the item startswith("MYTAB") , it'll be added to list l2
  2. It will find the index of MYTAB-2 inside the l2 list
  3. Remove MYTAB-2

Code:

l1 = ['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-2', 'MYTAB-PERF','ABC','DEF']
l2 = []

for item in l1:
    #add item to l2 if startswith MYTAB
    if item.startswith('MYTAB'):
        l2.append(item)

#getting index of 'MYTAB-2'
index = l2.index('MYTAB-2')

#removing MYTAB-B
del(l2[index])

#printing l2
l2

Output:

['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-PERF']

Use a filter with re on a list comprehension:

import re

l = ['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-2', 'MYTAB-PERF','ABC','DEF']
l = [i  for i in l if re.match(r'MYTAB-[^2]', i)]
print(l)
# ['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-PERF']
>>> import re >>> list_ = ['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-2', 'MYTAB-PERF', 'ABC', 'DEF'] >>> filtered_list = filter(lambda str_: re.search(r'^MYTAB(?!-2)', str_), list_) >>> filtered_list ['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-PERF']

You have a number of options for filtering the list. The basic answer to your question is 'MYTAB(?!-2)' if you are using re.match , which matches the beginning of the input.

import re
expr = re.compile('MYTAB(?!-2)')
rawList = ['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-2', 'MYTAB-PERF','ABC','DEF']
filteredList = [x for x in rawList if expr.match(x)]

However, there is a simpler way to do this since you are looking at a prefix:

filteredList = [x for x in rawList if x.startswith('MYTAB') and x not x.startswith('MYTAB-2')]

If you are not OK with list comprehensions for any reason, you may want to use the builtin filter function:

filteredList = list(filter(expr.match, rawList))

or even

filteredList = list(filter(lambda x: x.startswith('MYTAB') and not x.startswith('MYTAB-2'), rawList))

Additionally, if you do not wish to keep a reference to your precompiled expression around (eg, trading efficiency for brevity), you can use re.match instead of re.compile.match :

import re
rawList = ['MYTAB-EVENTS', 'MYTAB-1', 'MYTAB-2', 'MYTAB-PERF','ABC','DEF']
filteredList = [x for x in rawList if re.match(x)]

You can use the regex /MYTAB-[^2]+/ for this. The syntax [^2] means all except for 2 .

Try using pyregex to find your regex searches. Here is aa link to a working regex for your question

In this case it uses MYTAB-[^2] as the regex pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM