To filter list of strings by another list of strings in Python we can use the following code:
result = [x for x in strings1 if x in strings2]
But how can we filter list of substrings by another list of strings? For example:
substrings = ['a', 'b', 'c']
strings = ['_b_', '_c_', '_d_']
Result should be:
result = ['b', 'c']
解决方案
You can use something like that:
[x for x in substrings if [y for y in strings if x in y]]
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']