小编joh*_*red的帖子

Python3.在值字典中查找数组的值

我有两个清单.

列出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)

python dictionary python-3.x defaultdict

1
推荐指数
1
解决办法
70
查看次数

标签 统计

defaultdict ×1

dictionary ×1

python ×1

python-3.x ×1