Python中跳过NaN值的方法
在Python中,可以使用'math.isnan()'函数来检查一个值是否为NaN(Not a Number)。如果一个值为NaN,可以使用'continue'语句来跳过当前循环的剩余部分。
以下是一个示例代码:
import math
numbers = [1, 2, 3, float('nan'), 4, 5]
for num in numbers:
if math.isnan(num):
continue
print(num)
运行上述代码会输出:
1
2
3
4
5
可以看到,当循环到NaN时,会跳过该值并继续执行下一个循环。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/pa3v 著作权归作者所有。请勿转载和采集!