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

标签 python list python-2.7

返回此列表中第一个非 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/

相关文章:

python - 回调和 n 输入框小部件无法正常工作 Tkinter

python - 使用 Django 通过 gmail 发送电子邮件时出现 ConnectionRefusedError

java - 如何断言列表中的所有元素都在给定范围内

c# - 将 Dictionary<int, List<string>> 分配给空的新声明字典

python - 将列表的元素复制回 Python 中的列表

database - 使用 sqlalchemy 的 'engine_from_config'

python - 如何确定 matplotlib 轴是否已使用 axes.axis ('off' 关闭)?

python - Python中的正则表达式从右到左解析

python - 如何使用 SCP 或 SSH 在 Python (paramiko) 中递归地将完整目录复制到远程服务器?

python - 我在哪里可以获得模糊的 DLL,以便 py2exe 编译?