问题描述
“s and s.strip()”在filter中使用时,与单独使用结果不一样,且跟 and本身的用法矛盾。
一、在filter使用的代码块
def not_empty(s):
return s and s.strip()
print(list(filter(not_empty, ['A ','', ' B', None, 'C', ' ']))) #字符串A和B中都有空格
#运行结果为 ['A ',' B','C']
#字符串空格并没有去除
二、单独使用的代码块
def not_empty(s):
return s and s.strip()
print(not_empty('A ')) #字符串A中有空格
#运行结果为 'A'
#字符串空格去除了
疑问:
如果and连接的所有值都为真,应该返回最后一个真值。为什么在filter中使用的结果没有返回去除空格的字符串呢?求解答~