为什么不能使用if, lambda x: x if len(x.strip()) > 0 ???
为什么不能使用if, lambda x: x if len(x.strip()) > 0 ???
为什么不能使用if, lambda x: x if len(x.strip()) > 0 ???
2015-08-13
None没有strip属性,为嘛换个位置就报错
>>> print filter(lambda x: x and x.strip(),['test', None, '', 'st r', ' ', 'END'])
['test', 'st r', 'END']
>>> print filter(lambda x: x.strip() and x,['test', None, '', 'st r', ' ', 'END'])
Traceback (most recent call last):
File "<pyshell#58>", line 1, in <module>
print filter(lambda x: x.strip() and x,['test', None, '', 'st r', ' ', 'END'])
File "<pyshell#58>", line 1, in <lambda>
print filter(lambda x: x.strip() and x,['test', None, '', 'st r', ' ', 'END'])
AttributeError: 'NoneType' object has no attribute 'strip'
>>> print filter(lambda x: len(x.strip()) > 0 and x,['test',None, '', 'st r', ' ', 'END'])
Traceback (most recent call last):
File "<pyshell#70>", line 1, in <module>
print filter(lambda x: len(x.strip()) > 0 and x,['test',None, '', 'st r', ' ', 'END'])
File "<pyshell#70>", line 1, in <lambda>
print filter(lambda x: len(x.strip()) > 0 and x,['test',None, '', 'st r', ' ', 'END'])
AttributeError: 'NoneType' object has no attribute 'strip'
举报