如何并行查找嵌套列表中的元素
我有一个elements的列表a = ['a','b','c','d']
和嵌套列表b = [{'55','a a' },{'4','t'},{'x','y','zx'},{'b','d'},{'66','c'}]
> 。
此外,每个元素只能出现在b
的一个子列表中。
我想从a
中找到每个元素,从包含它的b
的子列表的索引([0,3,3,4,3]
)。假设来自a
的所有元素出现在b
中的某个地方。
对于我的数据大小,使用嵌套循环并不实用。
I have a list of elements A = ['a', 'b', 'c', 'd']
and a nested list B = [{'55','a'}, {'4','t'}, {'x','y','zx'}, {'b','d'}, {'66','c'}]
.
In addition, each element can appear only in one sub-list of B
.
I want to find, for each element from A
, the index of the sub-list from B
that contains it ([0, 3, 4, 3]
). Assume that all the elements from A
appear somewhere in B
.
Using a nested loop isn't practical for the size of my data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先为每个项目中的B中构建字典,然后在A上使用列表理解:
或者,如果您不确定A的所有元素都在B中:
输出:输出:
使用:使用:
First build a dictionary of the positions in B for each item, then use a list comprehension on A:
Or, if you are not sure that all elements of A are in B:
Output:
Used input: