delvins78
delvins78

Reputation: 43

How do I get a list of keys where the keys are found in some text?

I have a dictionary and some text:

dogs = {"jack russell": "benny", 
        "labrador": " oscar",
        "golden retriever": "ol' yeller"}

some_text = ["I have a jack russell and a golden retriever. They are 2 and 4 years old.", "My neighbour has a 3 cats."]

The output I want is:

[['jack russell', 'golden retriever'], [None]]

What I've got so far is:

some_list = []

for x in dogs.keys():
    matched_dogs = []
    for y in some_text:
        if x in y:
            matched_dogs.append(x)
        else:
            matched_dogs.append(None)
    some_list.append(matched_dogs)         

print(some_list)
[['jack russell', None], [None, None], ['golden retriever', None]]

My script is not quite there but surely there's a simple answer. Any nudge in the right direction appreciated.

Upvotes: 1

Views: 68

Answers (7)

Pedro Lobito
Pedro Lobito

Reputation: 98901

Late answer, but here's my 2c:

result = []
for text in some_text:
    temp = [k for k in dogs if k in text]
    if not temp:
        temp.append(None)
    result.append(temp)
print(result)
# [['jack russell', 'golden retriever'], [None]]

Demo

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148890

You can use a single line comprehension:

[x if len(x) > 0 else [None] for x in [[key for key in dogs if key in text]
                                       for text in some_text]]

With you data it gives as expected:

[['jack russell', 'golden retriever'], [None]]

Upvotes: 0

Ch3steR
Ch3steR

Reputation: 20669

You can use filter here.

>>>ans=[list(filter(lambda x: x in text,dogs)) for text in some_text]
# [['jack russell', 'golden retriever'], []]

Now replace [] with [None]

ans=[lst if lst else [None] for lst in ans]
# [['jack russell', 'golden retriever'], [None]]

Upvotes: 0

stars seven
stars seven

Reputation: 132

for y in some_text:
    matched_dogs = []
    for x in dogs.keys():
        if x in y:
            matched_dogs.append(x)
    if len(matched_dogs) == 0:
        matched_dogs.append(None)
    some_list.append(matched_dogs)

This is desired result?

Upvotes: 1

Scott Hunter
Scott Hunter

Reputation: 49803

Something a little more compact:

# Get OP's original result
some_list = [ [x for x in dogs.keys() if (x in y) ] for y in some_text]
# Fix the lines w/o any dogs
some_list = [z if z else [None] for z in some_list]

Upvotes: 1

chuck
chuck

Reputation: 1447

This gives the desired output:

res = [[], []]
for key in dogs:
    for text in some_text:
        if key in text:
            res[0].append(key)
            break
    else:
        res[1].append(None)

Note that an else after a for will only be executed if the for ended naturally, not through a break.

Upvotes: 1

Austin
Austin

Reputation: 26039

You can do an iteration over the list and check for the existence of keys in the elements, append to list if so else a None if nothing is found:

dogs = {"jack russell": "benny", 
        "labrador": " oscar",
        "golden retriever": "ol' yeller"}

some_text = ["I have a jack russell and a golden retriever. They are 2 and 4 years old.", "My neighbour has a 3 cats."]

res = []
for x in some_text:
    lst = []
    for k in dogs:
        if k in x:
            lst.append(k)
    if not lst:
        lst.append(None)
    res.append(lst)

print(res)

The output is exactly what is desired:

[['jack russell', 'golden retriever'], [None]]

Upvotes: 0

Related Questions