【发布时间】:2018-02-12 04:48:25
【问题描述】:
要在 Python 中通过另一个字符串列表过滤字符串列表,我们可以使用以下代码:
result = [x for x in strings1 if x in strings2]
但是我们如何通过另一个字符串列表过滤子字符串列表呢?例如:
substrings = ['a', 'b', 'c']
strings = ['_b_', '_c_', '_d_']
结果应该是:
result = ['b', 'c']
【问题讨论】:
-
带有简单的列表理解:
[i for i in substrings for j in strings if i in j]
标签: python arrays string list numpy