【问题标题】:Find the first instance of a nonzero number in a list in PythonPython在列表中找到非零数的第一个实例
【发布时间】:2013-10-30 09:48:56
【问题描述】:

我有一个这样的列表

myList = [0.0 , 0.0, 0.0, 2.0, 2.0]

我想找到列表中第一个不等于零的数字的位置。

myList.index(2.0)

在这个例子中有效,但有时第一个非零数字是 1 或 3。

有没有快速的方法?

【问题讨论】:

标签: python list


【解决方案1】:

nextenumerate 一起使用:

>>> 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

【讨论】:

  • numpy 数组的等价物是什么?
【解决方案2】:

使用filter

Python 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:(感谢 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

你必须处理特殊情况。

【讨论】:

  • 很好的选择,与next 的主要区别在于next 提供了一个默认值,而这会引发异常。
  • 对于python 3,您可以使用myList.index(next(filter(lambda x: x!=0, myList)))
  • 我最喜欢这个答案,但在 Matthias mod 中使用“下一个”。请注意,在任何一种情况下,根据具体情况,您应该首先考虑对 any(myList) 进行测试,以确保至少有一个非零值。它可能有助于程序流程和避免异常。
【解决方案3】:

您可以使用 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

【讨论】:

    【解决方案4】:

    这是一个单一的班轮:

    val = next((index for index,value in enumerate(myList) if value != 0), None)
    

    基本上,它使用next() 来查找第一个值,如果没有,则返回Noneenumerate() 用于创建一个迭代器,对索引、值元组进行迭代,以便我们知道我们所在的索引。

    【讨论】:

      【解决方案5】:

      当数组很大时,使用nextenumerate 非常好。对于较小的数组,我会使用来自numpyargmax,这样你就不需要循环了:

      import numpy as np
      
      myList = [0.0, 0.0, 0.0, 2.0, 2.0]
      myArray = np.array(myList)
      np.argmax(myArray > 0)
      3
      

      【讨论】:

      • 这不会找到第一个非零元素,例如对于[0, 0, 1, 2]
      【解决方案6】:

      这个怎么样:

      [i for i, x in enumerate(myList) if x][0]
      

      【讨论】:

        【解决方案7】:

        使用枚举怎么样?查看enumerate 文档。

        def first_non_zero(mylist):
          for index, number in enumerate(mylist):
            if number != 0: # or 'if number:'
              return index
        

        【讨论】:

          【解决方案8】:

          如何做以下事情:

          print (np.nonzero(np.array(myList))[0][0])
          

          这更方便,因为除了查找非零值外,这还有助于直接应用逻辑函数。例如:

          print (np.nonzero(np.array(myList)>1))
          

          【讨论】:

            【解决方案9】:

            只需使用列表推导:

            myDict = {x: index for index, x in enumerate(myList) if x}
            

            非零元素的索引是myDict[element]

            【讨论】:

            • 这并没有给出索引,它只是从列表中过滤掉非零元素。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-08-18
            • 2015-09-17
            • 2018-04-26
            • 1970-01-01
            • 1970-01-01
            • 2015-08-30
            • 1970-01-01
            相关资源
            最近更新 更多