简体   繁体   English

在列表中查找子列表的索引

[英]Find index of a sublist in a list

Trying to find the index of a sublists with an element. 试图找到带有元素的子列表的索引。 I'm not sure how to specify the problem exactly (which may be why I've overlooked it in a manual), however my problem is thus: 我不确定如何确切地指出问题(这可能就是为什么我在手册中忽略了它)的原因,但是我的问题是:

list1 = [[1,2],[3,4],[7,8,9]]

I want to find the first sub-list in list1 where 7 appears (in this case the index is 2, but lll could be very very long). 我想在出现1的list1中找到第一个子列表(在这种情况下,索引是2,但是lll可能会非常长)。 (It will be the case that each number will appear in only 1 sub-list – or not at all. Also these are lists of integers only) Ie a function like (在每种情况下,每个数字只会出现在1个子列表中-或根本不会出现。而且这些仅是整数列表),例如

spam = My_find(list1, 7)

would give spam = 2 I could try looping to make a Boolean index 将给垃圾邮件= 2我可以尝试循环以创建布尔索引

[7 in x for x in lll]

and then .index to find the 'true' - (as per Most efficient way to get indexposition of a sublist in a nested list ) However surely having to build a new boolean list is really inefficient.. 然后使用.index来查找'true'-(按照在嵌套列表中获取子列表的索引位置的最有效方法 )。但是,肯定要构建一个新的布尔列表确实效率不高。

My code starts with list1 being relatively small, however it keeps building up (eventually there will be 1 million numbers arranged in approx. 5000 sub-lists of list1 我的代码以list1相对较小开始,但是它不断建立(最终将有100万个数字排列在list1的大约5000个子列表中

Any thoughts? 有什么想法吗?

I could try looping to make a Boolean index 我可以尝试循环以创建布尔索引

[7 in x for x in lll]

and then .index to find the 'true' … However surely having to build a new boolean list is really inefficient 然后用.index来找到'true'…但是,肯定要建立一个新的布尔值列表确实效率很低

You're pretty close here. 您在这里很近。

First, to avoid building the list, use a generator expression instead of a list comprehension, by just replacing the [] with () . 首先,为避免构建列表,只需将[]替换为()使用生成器表达式而不是列表理解。

sevens = (7 in x for x in lll)

But how do you do the equivalent of .index when you have an arbitrary iterable, instead of a list? 但是,当您具有任意可迭代项而不是列表时,如何做等价于.index呢? You can use enumerate to associate each value with its index, then just filter out the non-sevens with filter or dropwhile or another generator expression, then next will give you the index and value of the first True . 您可以使用enumerate将每个值与其索引相关联,然后仅使用filterdropwhile或另一个生成器表达式过滤掉非零值,然后next将为您提供第一个True的索引和值。

For example: 例如:

indexed_sevens = enumerate(sevens)
seven_indexes = (index for index, value in indexed_sevens if value)
first_seven_index = next(seven_indexes)

You can of course collapse all of this into one big expression if you want. 当然,您可以根据需要将所有这些折叠成一个大表达式。

And, if you think about it, you don't really need that initial expression at all; 而且,如果您考虑一下,您根本就不需要该初始表达式; you can do that within the later filtering step: 您可以在以后的过滤步骤中执行以下操作:

first_seven_index = next(index for index, value in enumerate(lll) if 7 in value)

Of course this will raise a StopIteration exception instead of a ValueError expression if there are no sevens, but otherwise, it does the same thing as your original code, but without building the list, and without continuing to test values after the first match. 当然,如果没有七个,这将引发StopIteration异常,而不是ValueError表达式,但否则,它将执行与原始代码相同的操作,但不构建列表,并且在第一次匹配后不继续测试值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找另一个列表中每个子列表的超集索引 - Find the index of supersets for each sublist in another list 如何在列表的子列表中查找项目的索引(python) - How to Find the Index of an Item in a sublist of a list (python) 如何在Python中该列表的子列表中最小的列表中找到元素的索引? - How to find the index of an element in a list which is minimum in a sublist of that list in Python? 在嵌套列表中的所有子列表中查找子列表中相同的元素索引 - Find index of element in sublist that is same across all the sublists in a nested list 在列表python 3中查找子列表的索引 - Finding index of sublist in list python 3 在 Python 子列表中查找最小值索引 - min() 返回列表中最小值的索引 - Find index of minimum value in a Python sublist - min() returns index of minimum value in list 在python中查找列表的所有子列表 - find all sublist of list in python Python在子列表中查找列表长度 - Python find list lengths in a sublist 从嵌套列表中删除子列表(子列表的查找索引) - Remove sublist from nested list (finding index of the sublist) 在给定子列表的元素(Python)的情况下,查找列表中元素索引的最有效方法是什么 - What is the most efficient way to find the index of an element in a list, given only an element of a sublist (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM