return s and len(s.strip()) > 0 s是字符串不是布尔类型啊
return s and len(s.strip()) > 0 返回false或者true ,s是一个字符串,不是布尔类型啊,怎么会是false或者true呢
return s and len(s.strip()) > 0 返回false或者true ,s是一个字符串,不是布尔类型啊,怎么会是false或者true呢
2018-04-19
python与运算:只有两个布尔值都为 True 时,计算结果才为 True True and True # ==> True True and False # ==> False False and True # ==> False False and False # ==> False Python把0、空字符串''和None看成 False,其他数值和非空字符串都看成 True def is_not_empty(s): return s and len(s.strip()) > 0 filter(is_not_empty, ['test', None, '', 'str', ' ', 'END']) 结果:['test', 'str', 'END'] 因为s and len(s.strip()) 的结果大于0 所以len(s.strip())为true 所以s 为True ['test', None, '', 'str', ' ', 'END'] True-False-False-True-True-True >>> s=' ' >>> len(s.strip())0 所以符合条件的,长度大于0 且又是字符串 只有['test', 'str', 'END'] >>> [len(s) for s in ['test', 'str', 'END'] ] >>>[4, 3, 3] # 长度分别为 len(s.strip()) > 0
举报