我有一些列表,我想从中过滤元素。这是列表:
list1 = ['Little Mary had a lamb', 'the horse is black', 'Mary had a cat']
list2 = ['The horse is white', 'Mary had a dog', 'The horse is hungry']
listn = ...
假设我知道一个相关的单词或表达方式,下例中的Mary或horse。如果这些项目包含搜索的术语或表达式,我想获得一个新列表,哪些项目将从其他列表中提取。例如:
listMary = ['Little Mary had a lamb', 'Mary had a cat', 'Mary had a dog']
listHorse = ['the horse is black', 'The horse is white', 'The horse is hungry']
listn = ...
别担心我的数据更复杂;)
我知道我应该使用正则表达式模块,但在这种情况下我无法找到哪种方式。我在 Stack Overflow 上尝试了一些搜索,但我不知道如何足够清楚地表述问题,所以我找不到任何有用的东西。