Itay Moav -Malimovka
Itay Moav -Malimovka

Reputation: 53597

Search for a partial string in an array of strings

What is the Pythonic/quick way to search for a partial string in an array and then remove that string from the array?
(I can do it with a simple loop and IF IN and rebuild two array in the loop, Asking if there is a Pythonic way/function to do this)

Example:

array = ['rule1','rule2','exception[type_a]','rule3','exception[type_b]']
res(,)=remove_exceptions(array,'exception')
print(res[0]) >>> ['rule1','rule2','rule3']
print(res[1]) >>> ['exception[type_a]','exception[type_b]']

Upvotes: 2

Views: 1781

Answers (5)

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48067

You may use in-built filter with lambda function to achieve this as:

>>> my_array = array = ['rule1','rule2','exception[type_a]','rule3','exception[type_b]']
>>> my_string = 'exception'
>>> filter(lambda x: my_string not in x, my_array)
['rule1', 'rule2', 'rule3']

Upvotes: 0

Dimitris Fasarakis Hilliard
Dimitris Fasarakis Hilliard

Reputation: 160387

Seriously, just use a for loop, you're trying to create two lists so a single comprehension won't do (i.e the top solution so far iterates twice over the same list).

Create two lists and append to them conditionally:

l1 = list()
l2 = list()
for i in array:
    l1.append(i) if 'exception' in i else l2.append(i)

print(l1)
['exception[type_a]', 'exception[type_b]']
print(l2)
['rule1', 'rule2', 'rule3']

Upvotes: 1

Vivian
Vivian

Reputation: 1639

For list-of-strings array and thing-to-exclude target:

List comprehensions work:

result = [s for s in array if target not in s]

Or a generator comprehension for the same:

result = (s for s in array if target not in s)

(in is effectively a contains operator, and not in is the inverse.)

Alternately, use the filter() built-in with a lambda:

result = filter(lambda x: target not in x,
                array)

Either one returns a new object, rather than modifying your original list. The list comprehension returns a list, filter() returns a generator but you can wrap the call in list() if you need random access.

Upvotes: 0

kjaquier
kjaquier

Reputation: 854

>>> [x for x in array if 'exception' not in x]
['rule1', 'rule2', 'rule3']
>>> [x for x in array if 'exception' in x]
['exception[type_a]', 'exception[type_b]']

See also: Python: split a list based on a condition?

Upvotes: 2

Kasravnd
Kasravnd

Reputation: 107287

If you want to separate your items you can do it with one loop and by preserving the items in a dictionary:

>>> d = {}
>>> for i in array:
...     if 'exception' in i:
...         d.setdefault('exception', []).append(i)
...     else:
...         d.setdefault('other', []).append(i)
... 
>>> 
>>> d
{'exception': ['exception[type_a]', 'exception[type_b]'], 'other': ['rule1', 'rule2', 'rule3']}

You can access to separated items by calling the values of the dictionary:

>>> d.values()
[['exception[type_a]', 'exception[type_b]'], ['rule1', 'rule2', 'rule3']]

Upvotes: 1

Related Questions