Python 在列表中查找非零数字的第一个实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19502378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python find first instance of non zero number in list
提问by user2333196
I have a list like this
我有一个这样的清单
myList = [0.0 , 0.0, 0.0, 2.0, 2.0]
I would like to find the location of the first number in the list that is not equal to zero.
我想找到列表中第一个不等于零的数字的位置。
myList.index(2.0)
Works in this example but sometimes the first non zero number will be 1 or 3.
在此示例中有效,但有时第一个非零数字将是 1 或 3。
Is there a fast way of doing this?
有没有一种快速的方法来做到这一点?
采纳答案by Ashwini Chaudhary
Use next
with enumerate
:
使用next
有enumerate
:
>>> myList = [0.0 , 0.0, 0.0, 2.0, 2.0]
>>> next((i for i, x in enumerate(myList) if x), None) # x!= 0 for strict match
3
回答by Rushy Panchal
Simply use a list comprehension:
只需使用列表理解:
myDict = {x: index for index, x in enumerate(myList) if x}
The indices of the nonzero elements are myDict[element]
.
非零元素的索引是myDict[element]
。
回答by Eduardo
回答by Murph
Here's a one liner to do it:
这是一个单班轮来做到这一点:
val = next((index for index,value in enumerate(myList) if value != 0), None)
Basically, it uses next()to find the first value, or return None
if there isn't one. enumerate()is used to make an iterator that iterates over index,value tuples so that we know the index that we're at.
基本上,它使用next()来查找第一个值,None
如果没有则返回。enumerate()用于创建迭代索引、值元组的迭代器,以便我们知道我们所在的索引。
回答by Puffin GDI
Use filter
使用过滤器
Python 2:
蟒蛇2:
myList = [0.0, 0.0, 0.0, 2.0, 2.0]
myList2 = [0.0, 0.0]
myList.index(filter(lambda x: x!=0, myList)[0]) # 3
myList2.index(filter(lambda x: x!=0, myList2)[0]) # IndexError
Python 3: (Thanks for Matthias's comment):
Python 3:(感谢 Matthias 的评论):
myList.index(next(filter(lambda x: x!=0, myList))) # 3
myList2.index(next(filter(lambda x: x!=0, myList2))) # StopIteration
# from Ashwini Chaudhary's answer
next((i for i, x in enumerate(myList) if x), None) # 3
next((i for i, x in enumerate(myList2) if x), None) # None
You have to handle special case.
你必须处理特殊情况。
回答by Rodrigo Ferreira
You can use numpy.nonzero: http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.nonzero.html
您可以使用 numpy.nonzero:http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.nonzero.html
myList = [0.0 , 0.0, 0.0, 2.0, 2.0]
I = np.nonzero(myList)
#the first index is necessary because the vector is within a tuple
first_non_zero_index = I[0][0]
#3
回答by cjeiler
Using next
with enumerate
is excellent when the array is large. For smaller arrays, I would use argmax
from numpy
so that you won't need a loop:
当数组很大时,使用next
withenumerate
非常好。对于较小的数组,我会使用argmax
fromnumpy
这样你就不需要循环:
import numpy as np
myList = [0.0, 0.0, 0.0, 2.0, 2.0]
myArray = np.array(myList)
np.argmax(myArray > 0)
3
回答by SebMa
How'bout this :
这个怎么样:
[i for i, x in enumerate(myList) if x][0]
回答by devil in the detail
How about doing following:
如何执行以下操作:
print (np.nonzero(np.array(myList))[0][0])
This is more convenient because along with finding non-zero values, this can also help to apply logic function directly. For example:
这更方便,因为除了查找非零值外,这还有助于直接应用逻辑函数。例如:
print (np.nonzero(np.array(myList)>1))