查找另一个列表中每个子列表的超集索引 [英] Find the index of supersets for each sublist in another list

查看:65
本文介绍了查找另一个列表中每个子列表的超集索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在与另一个列表中的元素共享相同数据的元素列表中找到索引号.这些是我的清单:

I am trying to find the index number in a list of elements that share the same data with elements in another list. These are my lists:

list_A = [['A',1,'a',],['B',2,'b'],['C',3,'c'],['D',4,'d'],['E',5,'e'],['F',6,'f']]
list_B = [['A','a'],['D','d']['E','e']]

所需的输出应为:

[0,3,4]

我已经尝试过使用set()交集和index函数,但是无法对列表列表进行设置"操作.

I've tried using the set()intersection and the the index functions, but I couldn't make 'set' work with the list of lists.

推荐答案

您可以执行以下操作:

list_A = [['A', 1, 'a', ], ['B', 2, 'b'], ['C', 3, 'c'], ['D', 4, 'd'], ['E', 5, 'e'], ['F', 6, 'f']]
list_B = [['A', 'a'], ['D', 'd'], ['E', 'e']]

set_B = list(map(set, list_B))
result = [i for i, e in enumerate(list_A) if any(b.intersection(e) for b in set_B)]

print(result)

输出

[0, 3, 4]

这篇关于查找另一个列表中每个子列表的超集索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆