将列表列表与字典进行比较,并将输出作为元组列表的列表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将列表列表与字典进行比较,并将输出作为元组列表的列表相关的知识,希望对你有一定的参考价值。
我有一个字符串列表和一个字典列表:
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')]]
以上是关于将列表列表与字典进行比较,并将输出作为元组列表的列表的主要内容,如果未能解决你的问题,请参考以下文章