搜索字符串数组中的部分字符串

时间:2020-12-01 07:08:48

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)

在数组中搜索部分字符串,然后从数组中删除该字符串的python /quick方法是什么?(我可以用一个简单的循环来完成,如果在循环中并重新构建两个数组,询问是否存在一个python化的方法/函数来完成这个操作)

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]']

5 个解决方案

#1


2  

>>> [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?

请参见:Python:根据条件分割列表?

#2


1  

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']]

#3


1  

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).

认真地说,只需使用for循环,就会尝试创建两个列表,因此单个理解是不行的(i)。到目前为止,最上面的解在同一个列表上迭代两次)。

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']

#4


0  

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.)

(in实际上是一个包含算子,而in不是逆算子)

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

或者,使用内置lambda函数的filter():

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.

要么返回一个新对象,而不是修改原始列表。列表理解返回一个列表,filter()返回一个生成器,但是如果需要随机访问,可以将调用包装到list()中。

#5


0  

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

您可以使用lambda函数的内置过滤器来实现如下目的:

>>> 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']

#1


2  

>>> [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?

请参见:Python:根据条件分割列表?

#2


1  

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']]

#3


1  

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).

认真地说,只需使用for循环,就会尝试创建两个列表,因此单个理解是不行的(i)。到目前为止,最上面的解在同一个列表上迭代两次)。

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']

#4


0  

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.)

(in实际上是一个包含算子,而in不是逆算子)

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

或者,使用内置lambda函数的filter():

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.

要么返回一个新对象,而不是修改原始列表。列表理解返回一个列表,filter()返回一个生成器,但是如果需要随机访问,可以将调用包装到list()中。

#5


0  

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

您可以使用lambda函数的内置过滤器来实现如下目的:

>>> 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']