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