[英]Find elements of a list that contain substrings from another list in Python
In my code, I have two lists, of different lenghts, which I'll call "main" and "secondary". 在我的代码中,我有两个列表,每个列表的长度不同,分别称为“主要”和“次要”。 I need to use elements in secondary to select elements in main.
我需要在辅助元素中使用元素来选择主要元素。 However, secondary contains elements that are just sub-sets of the strings in main.
但是,secondary包含的元素只是main中字符串的子集。 In code:
在代码中:
main = ["pinecone", "treeleaf", "dishwasher"]
secondary = ["pine", "washer", "unrelated", "flowerbed"]
Usually secondary is much longer than main (I mention this in case solutions involve performance penalties). 通常,中学的时间要比中学的长得多(在解决方案涉及性能损失的情况下,我会提到这一点)。 How do I go and select elements in "main" basing on "secondary" with the most efficient (and Pythonic) way possible?
如何以最有效的方式(和Pythonic方式)在“主要”基础上选择“主要”元素? If it were a function, I'd expect
如果它是一个功能,我希望
>>> selected_items = select_items(main, secondary)
>>> print selected_items
["pinecone", "dishwasher"]
Thanks! 谢谢!
Naive approach: 天真的方法:
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']
A similar approach works when your main list and secondary list are the same: 当您的主列表和辅助列表相同时,可以使用类似的方法:
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']
Note, you can get the partially matching string instead (or even both!): 请注意,您可以改为获取部分匹配的字符串(或什至两者!):
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')]
def select_items(strings, substrs):
return [m for m in strings if any(s in m for s in substrs)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.