在 Python 中的列表中查找子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13779526/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:34:57  来源:igfitidea点击:

Finding a substring within a list in Python

pythonstringlist

提问by frankV

Background:

背景:

Example list: mylist = ['abc123', 'def456', 'ghi789']

示例列表: mylist = ['abc123', 'def456', 'ghi789']

I want to retrieve an element if there's a match for a substring, like abc

如果子字符串匹配,我想检索一个元素,例如 abc

Code:

代码:

sub = 'abc'
print any(sub in mystring for mystring in mylist)

above prints Trueif any of the elements in the list contain the pattern.

True如果列表中的任何元素包含该模式,则上面打印。

I would like to print the element which matches the substring. So if I'm checking 'abc'I only want to print 'abc123'from list.

我想打印与子字符串匹配的元素。因此,如果我正在检查,'abc'我只想'abc123'从列表中打印。

采纳答案by David Robinson

print [s for s in list if sub in s]

If you want them separated by newlines:

如果您希望它们由换行符分隔:

print "\n".join(s for s in list if sub in s)

Full example, with case insensitivity:

完整示例,不区分大小写:

mylist = ['abc123', 'def456', 'ghi789', 'ABC987', 'aBc654']
sub = 'abc'

print "\n".join(s for s in mylist if sub.lower() in s.lower())

回答by unutbu

Use a simple forloop:

使用一个简单的for循环:

seq = ['abc123', 'def456', 'ghi789']
sub = 'abc'

for text in seq:
    if sub in text:
        print(text)

yields

产量

abc123

回答by Hyperboreus

This prints all elements that contain sub:

这将打印包含 sub 的所有元素:

for s in filter (lambda x: sub in x, list): print (s)

回答by oathead

I'd just use a simple regex, you can do something like this

我只是使用一个简单的正则表达式,你可以做这样的事情

import re
old_list = ['abc123', 'def456', 'ghi789']
new_list = [x for x in old_list if re.search('abc', x)]
for item in new_list:
    print item

回答by Frank Zalkow

All the answers work but they always traverse the whole list. If I understand your question, you only need the first match. So you don't have to consider the rest of the list if you found your first match:

所有答案都有效,但它们总是遍历整个列表。如果我理解你的问题,你只需要第一场比赛。因此,如果您找到第一个匹配项,则不必考虑列表的其余部分:

mylist = ['abc123', 'def456', 'ghi789']
sub = 'abc'
next((s for s in mylist if sub in s), None) # returns 'abc123'

If the match is at the end of the list or for very small lists, it doesn't make a difference, but consider this example:

如果匹配位于列表的末尾或非常小的列表,则没有区别,但请考虑以下示例:

import timeit

mylist = ['abc123'] + ['xyz123']*1000
sub = 'abc'

timeit.timeit('[s for s in mylist if sub in s]', setup='from __main__ import mylist, sub', number=100000)
# for me 7.949463844299316 with Python 2.7, 8.568840944994008 with Python 3.4
timeit.timeit('next((s for s in mylist if sub in s), None)', setup='from __main__ import mylist, sub', number=100000) 
# for me 0.12696599960327148 with Python 2.7, 0.09955992100003641 with Python 3.4