在Python中查找包含其他列表子字符串的列表元素
在我的代码中,我有两个不同长度的列表,我称之为“main”和“secondary”。我需要使用secondary中的元素来选择main中的元素。然而,次要元素只包含主元素中字符串的子集。代码:在Python中查找包含其他列表子字符串的列表元素,python,list,Python,List,在我的代码中,我有两个不同长度的列表,我称之为“main”和“secondary”。我需要使用secondary中的元素来选择main中的元素。然而,次要元素只包含主元素中字符串的子集。代码: main = ["pinecone", "treeleaf", "dishwasher"] secondary = ["pine", "washer", "unrelated", "flowerbed"] 通常次要的比主要的长很多(我在解决方案涉及性能损失的情况下提到这一点)。 如何以最有效的方式(和P
main = ["pinecone", "treeleaf", "dishwasher"]
secondary = ["pine", "washer", "unrelated", "flowerbed"]
通常次要的比主要的长很多(我在解决方案涉及性能损失的情况下提到这一点)。
如何以最有效的方式(和Pythonic的方式)在“主”和“次”的基础上选择元素?如果它是一个函数,我希望
>>> selected_items = select_items(main, secondary)
>>> print selected_items
["pinecone", "dishwasher"]
谢谢 天真的方法:
def select_items(strings, substrs):
return [m for m in strings if any(s in m for s in substrs)]
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']
当主列表和次列表相同时,类似的方法也适用:
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')]