Finding a substring within a list in Python
本问题已经有最佳答案,请猛点这里访问。
背景:
示例列表:EDOCX1[0]
我想检索一个元素,如果有一个匹配的子字符串,如
1 2 | sub = 'abc' print any(sub in mystring for mystring in mylist) |
如果列表中的任何元素包含模式,则上面打印
我想打印与子字符串匹配的元素。所以如果我检查
1 | print [s for s in list if sub in s] |
如果要用换行符分隔它们:
1 2 | print" ".join(s for s in list if sub in s) |
完整示例,不区分大小写:
1 2 3 4 5 | mylist = ['abc123', 'def456', 'ghi789', 'ABC987', 'aBc654'] sub = 'abc' print" ".join(s for s in mylist if sub.lower() in s.lower()) |
所有答案都有效,但它们总是贯穿整个列表。如果我理解你的问题,你只需要第一场比赛。因此,如果你找到了第一个匹配项,就不必考虑列表的其余部分:
1 2 3 | mylist = ['abc123', 'def456', 'ghi789'] sub = 'abc' next((s for s in mylist if sub in s), None) # returns 'abc123' |
如果匹配在列表的末尾或非常小的列表中,则不会有什么不同,但请考虑以下示例:
1 2 3 4 5 6 7 8 9 | 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 |
使用一个简单的
1 2 3 4 5 6 | seq = ['abc123', 'def456', 'ghi789'] sub = 'abc' for text in seq: if sub in text: print(text) |
产量
1 | abc123 |
我只需要一个简单的正则表达式,你可以做这样的事情
1 2 3 4 5 | 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 |
这将打印包含Sub的所有元素:
1 | for s in filter (lambda x: sub in x, list): print (s) |