简体   繁体   English

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

[英]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.

相关问题 查找包含来自另一个列表的子字符串的列表元素的有效方法 - An efficient way to find elements of a list that contain substrings from another list 有没有办法检查列表中的字符串是否包含来自另一个列表的任何子字符串? - Is there a way to check if strings in a list contain any substrings from another list? Python 从包含其他列表的子字符串的列表中删除元素 - Python remove elements from a list that contains substrings from other list Python,检查列表中是否包含其他列表中的元素,并进行有效比较 - Python, check if a list contain elements from another list and compare them efficiently 如何根据列表元素是否包含 Python 中另一个列表中的 substring 来过滤掉列表元素 - How to filter out list elements based on if they contain a substring from another list in Python 检查列表中的字符串是否包含另一个列表中的所有元素 - Check if string in list contain all the elements from another list 从python中的一长串字符串中查找并删除一些子字符串 - find and remove some substrings from a long list of string in python 使用另一个列表在成对元素列表中查找元素(Python) - Find elements in a paired elements list using another list (Python) Python - 查找与另一个列表的元素匹配的列表元素的索引 - Python - Find the indices of elements of a list that match elements of another list 删除列表中所有属于python中列表其他元素子字符串的元素 - Remove all elements of a list that are substrings of other elements of the list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM