In [1]: from numpy import math
In [2]: a = NoneIn [3]: not aOut[3]: True
In [4]: len(a or ()) == 0Out[4]: True
In [5]: a == NoneOut[5]: True
In [6]: a is NoneOut[6]: True
In [7]: a != aOut[7]: False
In [9]: math.isnan(a)Traceback (most recent call last):File "<ipython-input-9-6d4d8c26d370>", line 1, in <module>math.isnan(a)TypeError: a float is required
In [10]: len(a) == 0Traceback (most recent call last):File "<ipython-input-10-65b72372873e>", line 1, in <module>len(a) == 0TypeError: object of type 'NoneType' has no len()
NaN型
In [11]: b = float('nan')In [12]: bOut[12]: nan
In [13]: not bOut[13]: False
In [14]: b != bOut[14]: True
In [15]: math.isnan(b)Out[15]: True
if not pd.isnull(atext):for word in nltk.word_tokenize(atext):
NLTK的特征提取功能
def act_features(atext):features = {}if not pd.isnull(atext):for word in nltk.word_tokenize(atext):if word not in default_stopwords:features['cont({})'.format(word.lower())]=Truereturn features
from math import isnan
Z = ['a','b', float('NaN'), 'd', float('1.1024')]
[x for x in Z if not (type(x) == float # let's drop all float values…and isnan(x) # … but only if they are nan)]
['a', 'b', 'd', 1.1024]
Short-circuit evaluation means that isnan will not be called on values that are not of type 'float', as False and (…) quickly evaluates to False without having to evaluate the right-hand side.
import pandas as pdimport numpy as npimport math
# For single variable all three libraries return single booleanx1 = float("nan")
print(f"It's pd.isna: {pd.isna(x1)}")print(f"It's np.isnan: {np.isnan(x1)}}")print(f"It's math.isnan: {math.isnan(x1)}}")