我有一个字符串列表和一个字典列表:
docsp = [['how', 'can', 'I', 'change', 'this', 'car'], ['I', 'can', 'delete', 'this', 'module']]
ent = {'change' : 'action', 'car' : 'item', 'delete' : 'action'}
我想将字典与列表列表进行比较,并将列表元素与字典键值对标记为新的元组列表,如下所示。
newlist = [[('how', 'O'), ('can', 'O'), ('I', 'O'), ('change', 'action'), ('this', 'O'), ('car', 'item')], [('I', 'O'), ('can', 'O'), ('delete', 'action'), ('this', 'O'), ('module', 'O')]]
我尝试了以下代码:
n = []
for k,v in ent.items():
for i in docsp:
for j in i:
if j==k:
n.append((j,v))
n.append((j, 'O'))
n
并获得以下输出:
[('how', 'O'),
('can', 'O'),
('I', 'O'),
('change', 'action'),
('change', 'O'),
('this', 'O'),
('car', 'O'),
('I', 'O'),
('can', 'O'),
('delete', 'O'),
('this', 'O'),
('module', 'O'),
('how', 'O'),
('can', 'O'),
('I', 'O'),
('change', 'O'),
('this', 'O'),
('car', 'item'),
('car', 'O'),
('I', 'O'),
('can', 'O'),
('delete', 'O'),
('this', 'O'),
('module', 'O'),
('how', 'O'),
('can', 'O'),
('I', 'O'),
('change', 'O'),
('this', 'O'),
('car', 'O'),
('I', 'O'),
('can', 'O'),
('delete', 'action'),
('delete', 'O'),
('this', 'O'),
('module', 'O')]
我经历了这个link,但无法修改它以获得我的预期输出。
这是一个利用列表理解的解决方案:
docsp = [['how', 'can', 'I', 'change', 'this', 'car'], ['I', 'can', 'delete', 'this', 'module']]
ent = {'change' : 'action', 'car' : 'item', 'delete' : 'action'}
result = [[(docsp[i][j], ent.get(docsp[i][j], 'O')) for j in range(len(docsp[i]))] \
for i in range(len(docsp))]
# [[('how', 'O'),
# ('can', 'O'),
# ('I', 'O'),
# ('change', 'action'),
# ('this', 'O'),
# ('car', 'item')],
# [('I', 'O'),
# ('can', 'O'),
# ('delete', 'action'),
# ('this', 'O'),
# ('module', 'O')]]