Python |基于子字符串列表过滤字符串列表

原文:https://www . geesforgeks . org/python-filter-list-基于字符串的子字符串列表/

给定两个字符串列表字符串子字符串,编写一个 Python 程序过滤掉字符串中包含子字符串中字符串的所有字符串。

示例:

Input : string = ['city1', 'class5', 'room2', 'city2']
        substr = ['class', 'city']
Output : ['city1', 'class5', 'city2']

Input : string = ['coordinates', 'xyCoord', '123abc']
        substr = ['abc', 'xy']
Output : ['xyCoord', '123abc']

方法#1 : 使用列表理解

我们可以在运算符中使用列表理解和来检查“substr”中的字符串是否包含在“string”中。

# Python3 program to Filter list of 
# strings based on another list
import re

def Filter(string, substr):
    return [str for str in string if
             any(sub in str for sub in substr)]

# Driver code
string = ['city1', 'class5', 'room2', 'city2']
substr = ['class', 'city']
print(Filter(string, substr))

Output:

['city1', 'class5', 'city2']

方法 2 : Python 正则表达式

# Python3 program to Filter list of 
# strings based on another list
import re

def Filter(string, substr):
    return [str for str in string 
    if re.match(r'[^\d]+|^', str).group(0) in substr]

# Driver code
string = ['city1', 'class5', 'room2', 'city2']
substr = ['class', 'city']
print(Filter(string, substr))

Output:

['city1', 'class5', 'city2']