Manthan Jamdagni
Manthan Jamdagni

Reputation: 1031

Efficient way to find if it is possible to create a word from a list by using words from another list

I have two lists one is expected_entities and other is extracted_words. The list expected_entities is comprised of strings which can each contain multiple words or a single word. While the list extracted_words contains strings with single words. I need to find out if any expected_entity from expected_entities can be constructed using multiple or single entries from extracted_words.

For example,

expected_entities = {"hello joe", "hi julie", "hola sam", "paul"}
extracted_words = {"hello", "hi", "hola", "sam"}

My code should return hola sam as the output.

Currently, my solution looks like this:

def my_method(expected_entities, extracted_words):
    for expected_entity in expected_entities:
        words = expected_entity.split()
        if set(words) < set(extracted_words):
            return expected_entity

I wanted to know if there can be a better solution to achieve the same this doesn't seem to work fine.

Note - Please assume that the data will be such that only one expected_entity could be created from extracted_words.

Upvotes: 0

Views: 30

Answers (1)

BcK
BcK

Reputation: 2821

Not very efficient for now, but this will get the job done.

expected_entities = {"hello joe", "hi julie", "hola sam", "paul"}
extracted_words = {"hello", "hi", "hola", "sam"}

def my_method(expected_entities, extracted_words):
    for expected_entity in expected_entities:
        words = expected_entity.split()
        if all(word in extracted_words for word in words):
            return expected_entity

my_method(expected_entities, extracted_words)

# 'hola sam'

Upvotes: 1

Related Questions