[英]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
将每个值与其索引相关联,然后仅使用filter
或dropwhile
或另一个生成器表达式过滤掉非零值,然后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.