且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

返回 python 列表中的第一个非 NaN 值

更新时间:2022-12-02 23:00:17

如果你经常做这件事,把它放到一个函数中,使其可读性更强:

If you're doing it a lot, put it into a function to make it readable and easy:

import math

t = [float('nan'), float('nan'), 5.5, 5.0, 5.0, 5.5, 6.0, 6.5]

def firstNonNan(listfloats):
  for item in listfloats:
    if math.isnan(item) == False:
      return item

firstNonNan(t)
5.5