删除None或者空字符串时出错
def is_not_empty(s):
return len(s.strip()) > 0
print(list(filter(is_not_empty, ['hahah', 'jiayou', 'keyide', ' ', None, 'none'])))
解决办法:
# 删除None或者空字符串
def is_not_empty(s):
return s and len(s.strip()) > 0 # 在此处进行了修改
print(list(filter(is_not_empty, ['hahah', 'jiayou', 'keyide', ' ', None, 'none'])))
结果为:
[‘hahah’, ‘jiayou’, ‘keyide’, ‘none’]