Search code examples
pythonpython-3.xdictionarydefaultdict

Python3. Find values of array in values dictionary


I have two lists.

List all_text - the first value is the key, the second meaning is a set of words.

List keyword_list - list of keywords I want to find in a set of words all_text.

My code shows all values from list all_text.

I want to get the following results:

defaultdict(<class 'list'>, {'Z1234': ['earth'], 'Z1207': ['north']})

How to fix my code below?

from collections import defaultdict, Counter
all_text = [['Z1234', 'earth total surface area land'], ['Z1207', 'first 
north university']]
keyword_list = ['earth', 'north']

dictions = defaultdict(list)
for key, sentence in all_text:
    dictions[key].extend(sentence.split())

result = defaultdict(list)
for x in dictions.values():
    for i in x:
        for y in keyword_list:
            if i in y:
                result[key].extend(x)
print(result)

>>defaultdict(<class 'list'>, {'Z1207': ['first', 'north', 'university', 
'earth', 'total', 'surface', 'area', 'land']})

Solution

  • Here is one way.

    from collections import defaultdict
    
    all_text = [['Z1234', 'earth total surface area land'],
                ['Z1207', 'first north university']]
    keyword_list = ['earth', 'north']
    
    keyword_set = set(keyword_list)
    
    d = defaultdict(list)
    
    for k, v in all_text:
        for w in set(v.split()) & keyword_set:
            d[k].append(w)
    
    # defaultdict(list, {'Z1207': ['north'], 'Z1234': ['earth']})
    

    Explanation

    • str.split with no argument separates a string in to a list of words by whitespace.
    • & is alternative syntax for set intersection.