繁体   English   中英

关于 python 中的过滤器 function 的问题

[英]questions about filter function in python

def empty(s):
    return s and s.strip()
print(list(filter(empty,['A','B',' C '])))

Operation results :['A', 'B', ' C '] I was so confused about " C " In my opinion: s.strip() cause to delete space of the " C " and return "C",function will返回false,过滤器将消除它。 所以结果应该是['A','B']?

我的英语不好,我想把困惑说清楚。

如果要消除其中包含空格的所有值,为什么不使用以下 function:

def no_spaces(s):
# checks if there is a space in s
    if " " in s:
        return False
    return True

print(list(filter(no_spaces,['A','B',' C '])))  # returns ['A', 'B']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM