There's a math.isnan()
which could be used to identify the NaN
s. You can use this inside list comprehension to achieve this as:
>>> import math
>>> lst = [1.2,2.3,float('nan')]
>>> [i for i in lst if not math.isnan(i)]
[1.2, 2.3]
Additionally, one of the property of 'NaN'
numbers is that they return False
when compared with itself. You can also utilise this property to identify them and filter them out as:
>>> [i for i in lst if i==i]
[1.2, 2.3]
Refer Why is NaN not equal to NaN? for more details.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…