交叉匹配两个列表

use*_*030 1 python

我有两个列表,我试图看看两个列表中的元素中的子串之间是否存在匹配.

["Po2311tato","Pin2231eap","Orange2231edg","add22131dfes"]
["2311","233412","2231"]
Run Code Online (Sandbox Code Playgroud)

如果元素中的任何子字符串与第二个列表匹配,例如"Po2311tato"将与"2311"匹配.然后我想把"Po2311tato"放在一个新列表中,其中匹配的第一个元素将放在新列表中.所以新的名单将是["Po2311tato","Pin2231eap","Orange2231edg"]

小智 5

您可以使用语法'substring' in string执行此操作:

a = ["Po2311tato","Pin2231eap","Orange2231edg","add22131dfes"]
b = ["2311","233412","2231"]

def has_substring(word):
    for substring in b:
        if substring in word:
            return True
    return False

print filter(has_substring, a)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!