是否可以通过 Python 中的另一个字符串列表过滤子字符串列表?

Dmi*_*try 1 python arrays string numpy list

要通过 Python 中的另一个字符串列表过滤字符串列表,我们可以使用以下代码:

result = [x for x in strings1 if x in strings2]
Run Code Online (Sandbox Code Playgroud)

但是我们如何通过另一个字符串列表来过滤子字符串列表呢?例如:

substrings = ['a', 'b', 'c']
strings = ['_b_', '_c_', '_d_']
Run Code Online (Sandbox Code Playgroud)

结果应该是:

result = ['b', 'c']
Run Code Online (Sandbox Code Playgroud)

Dek*_*kel 5

你可以使用这样的东西:

[x for x in substrings if [y for y in strings if x in y]]
Run Code Online (Sandbox Code Playgroud)


In [1]: substrings = ['a', 'b', 'c']

In [2]: strings = ['_b_', '_c_', '_d_']

In [3]: [x for x in substrings if [y for y in strings if x in y]]
Out[3]: ['b', 'c']
Run Code Online (Sandbox Code Playgroud)