从这两个清单中:
list_A = ["eyes", "clothes", "body" "etc"]
list_B = ["xxxx_eyes", "xxx_zzz", "xxxxx_bbbb_zzzz_clothes" ]
我想用第二个列表中的那些对象填充第三个列表,只要他的名字的某些部分与第一个列表中的一个名称匹配。
在前面的示例中,第三个列表必须是:
["xxxx_eyes", "xxxxx_bbbb_zzzz_clothes"]
答案 0 :(得分:3)
如果你想使用列表理解,这将有效:
list_C = [word for word in list_B if any(test in word for test in list_A)]
答案 1 :(得分:2)
如果你想使用正则表达式:
search = re.compile("|".join(map(re.escape, list_A))).search
result = filter(search, list_B)
虽然在大多数情况下Blender的回答可能已经足够了。
答案 2 :(得分:1)
In [1]: list_A = ["eyes", "clothes", "body" "etc"]
In [2]: list_B = ["xxxx_eyes", "xxx_zzz", "xxxxx_bbbb_zzzz_clothes" ]
In [7]: [x for x in list_B if any(y in list_A for y in x.split('_'))]
Out[7]: ['xxxx_eyes', 'xxxxx_bbbb_zzzz_clothes']
答案 3 :(得分:0)
最慢但最简单的是:
list_A = ["eyes", "clothes", "body" "etc"]
list_B = ["xxxx_eyes", "xxx_zzz", "xxxxx_bbbb_zzzz_clothes" ]
list_C=[]
for _ in list_A:
for __ in list_B:
if _ in __:
list_C.append(__)