joh*_*red 1 python dictionary python-3.x defaultdict
我有两个清单.
列出all_text - 第一个值是键,第二个含义是一组词.
列出keyword_list - 我想在一组单词all_text中找到的关键字列表.
我的代码显示list all_text中的所有值.
我想得到以下结果:
defaultdict(<class 'list'>, {'Z1234': ['earth'], 'Z1207': ['north']})
Run Code Online (Sandbox Code Playgroud)
如何解决我的代码?
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']})
Run Code Online (Sandbox Code Playgroud)
这是一种方式.
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']})
Run Code Online (Sandbox Code Playgroud)
说明