在Python中查找包含来自另一个列表的子字符串的列表元素

时间:2011-07-26 10:28:10

标签: python list

在我的代码中,我有两个不同长度的列表,我将其称为“主要”和“次要”。我需要在secondary中使用元素来选择main中的元素。但是,secondary包含的元素只是main中字符串的子集。在代码中:

main = ["pinecone", "treeleaf", "dishwasher"]
secondary = ["pine", "washer", "unrelated", "flowerbed"]

通常次要比主要长得多(我提到这个解决方案涉及性能损失)。 如何以最高效(和Pythonic)方式在“辅助”基础上选择“main”中的元素?如果它是一个函数,我期待

>>> selected_items = select_items(main, secondary)
>>> print selected_items
["pinecone", "dishwasher"]

谢谢!

3 个答案:

答案 0 :(得分:4)

天真的方法:

In [2]: main = ["pinecone", "treeleaf", "dishwasher"]

In [3]: secondary = ["pine", "washer", "unrelated", "flowerbed"]

In [4]: [x for x in main if any(x in y or y in x for y in secondary)]
Out[4]: [u'pinecone', u'dishwasher']

答案 1 :(得分:0)

def select_items(strings, substrs):
    return [m for m in strings if any(s in m for s in substrs)]

答案 2 :(得分:0)

当主列表和辅助列表相同时,类似的方法也适用:

In [2]: main = ["pinecone", "treeleaf", "dishwasher"] + ["pine", "washer", "unrelated", "flowerbed"]

In [4]: [x for x in main for y in main if y in x and x != y]
Out[4]: ['pinecone', 'dishwasher']

请注意,您可以改为获取部分匹配的字符串(或什至两者!):

In [5]: [y for x in main for y in main if y in x and x != y]
Out[5]: ['pine', 'washer']

In [6]: [(y,x) for x in main for y in main if y in x and x != y]
Out[6]: [('pine', 'pinecone'), ('washer', 'dishwasher')]