3

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. 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? If it were a function, I'd expect

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

Thanks!

3 Answers 3

6

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']
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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')]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.