检查 Python 列表项是否包含另一个字符串中的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4843158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a Python list item contains a string inside another string
提问by SandyBr
I have a list:
我有一个清单:
my_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
and want to search for items that contain the string 'abc'. How can I do that?
并且想要搜索包含字符串的项目'abc'。我怎样才能做到这一点?
if 'abc' in my_list:
would check if 'abc'exists in the list but it is a part of 'abc-123'and 'abc-456', 'abc'does not exist on its own. So how can I get all items that contain 'abc'?
将检查'abc'列表中是否存在,但它是'abc-123'and的一部分'abc-456','abc'本身不存在。那么我怎样才能获得包含的所有项目'abc'呢?
采纳答案by Sven Marnach
If you only want to check for the presence of abcin any string in the list, you could try
如果您只想检查abc列表中任何字符串中的存在,您可以尝试
some_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
if any("abc" in s for s in some_list):
# whatever
If you really want to get all the items containing abc, use
如果您真的想获取包含的所有项目abc,请使用
matching = [s for s in some_list if "abc" in s]
回答by Mariy
x = 'aaa'
L = ['aaa-12', 'bbbaaa', 'cccaa']
res = [y for y in L if x in y]
回答by Imran
any('abc' in item for item in mylist)
回答by MAK
Use filterto get at the elements that have abc.
使用filter以获取该具备的要素abc。
>>> lst = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
>>> print filter(lambda x: 'abc' in x, lst)
['abc-123', 'abc-456']
You can also use a list comprehension.
您还可以使用列表理解。
>>> [x for x in lst if 'abc' in x]
By the way, don't use the word listas a variable name since it is already used for the listtype.
顺便说一句,不要使用这个词list作为变量名,因为它已经用于list类型。
回答by Rubycon
for item in my_list:
if item.find("abc") != -1:
print item
回答by Robert Muil
This is quite an old question, but I offer this answer because the previous answers do not cope with items in the list that are not strings (or some kind of iterable object). Such items would cause the entire list comprehension to fail with an exception.
这是一个很老的问题,但我提供这个答案是因为之前的答案没有处理列表中不是字符串(或某种可迭代对象)的项目。这些项目会导致整个列表理解失败并出现异常。
To gracefully deal with such items in the list by skipping the non-iterable items, use the following:
要通过跳过不可迭代的项目来优雅地处理列表中的此类项目,请使用以下命令:
[el for el in lst if isinstance(el, collections.Iterable) and (st in el)]
then, with such a list:
然后,有了这样一个列表:
lst = [None, 'abc-123', 'def-456', 'ghi-789', 'abc-456', 123]
st = 'abc'
you will still get the matching items (['abc-123', 'abc-456'])
你仍然会得到匹配的项目 ( ['abc-123', 'abc-456'])
The test for iterable may not be the best. Got it from here: In Python, how do I determine if an object is iterable?
iterable 的测试可能不是最好的。从这里得到它:在 Python 中,如何确定对象是否可迭代?
回答by fantabolous
Just throwing this out there: if you happen to need to match against more than one string, for example abcand def, you can combine two comprehensions as follows:
只是把它扔在那里:如果您碰巧需要匹配多个字符串,例如abcand def,您可以按如下方式组合两个推导式:
matchers = ['abc','def']
matching = [s for s in my_list if any(xs in s for xs in matchers)]
Output:
输出:
['abc-123', 'def-456', 'abc-456']
回答by Iulian
From my knowledge, a 'for' statement will always consume time.
据我所知,'for' 语句总是会消耗时间。
When the list length is growing up, the execution time will also grow.
当列表长度增加时,执行时间也会增加。
I think that, searching a substring in a string with 'is' statement is a bit faster.
我认为,使用 'is' 语句在字符串中搜索子字符串要快一些。
In [1]: t = ["abc_%s" % number for number in range(10000)]
In [2]: %timeit any("9999" in string for string in t)
1000 loops, best of 3: 420 μs per loop
In [3]: %timeit "9999" in ",".join(t)
10000 loops, best of 3: 103 μs per loop
But, I agree that the anystatement is more readable.
但是,我同意该any声明更具可读性。
回答by RogerS
If you just need to know if 'abc' is in one of the items, this is the shortest way:
如果您只需要知道 'abc' 是否在其中一项中,这是最短的方法:
if 'abc' in str(my_list):
回答by chandra
my_list = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
for item in my_list:
if (item.find('abc')) != -1:
print ('Found at ', item)

