返回此列表中第一个非 nan 值的最佳方法是什么?
testList = [nan, nan, 5.5, 5.0, 5.0, 5.5, 6.0, 6.5]
编辑:
nan 是一个 float
最佳答案
您可以使用 next
, 一个 generator expression , 和 math.isnan
:
>>> from math import isnan
>>> testList = [float('nan'), float('nan'), 5.5, 5.0, 5.0, 5.5, 6.0, 6.5]
>>> next(x for x in testList if not isnan(x))
5.5
>>>
关于python - 返回 python 列表中的第一个非 NaN 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22129495/