Python find first instance of non zero number in list

后端 未结 9 1322
旧巷少年郎
旧巷少年郎 2020-12-30 19:59

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 e

9条回答
  •  时光取名叫无心
    2020-12-30 20:12

    What about using enumerate? Check the enumerate documentation.

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

提交回复
热议问题