python对方法的迭代

欢迎关注我的博客Zhuhao's Blog获取最新文章

any()

Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:
(返回True,如果iterable中的任何一个element是True。如果iterable是空的,返回False。等价与:)

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

对一个可迭代的对象,要想知道其中某一个元素是否符合某一个表达式,用 any(),最好不过了。

for

今天才知道,原来for循环还可以对方法进行循环,尽管之前就知道list中什么都可以放,可是放方法的还是头会见到,学习的路果然是任重而道远。

实际运用

问题:
You are given a string S.
Your task is to find out if the string S contains: alphanumeric characters, alphabetical characters, digits, lowercase and uppercase characters.

给你一个string S。
你的任务是判断string S是否包含: 数字字母都有 按字母表顺序(Unicode) 有数字 有小写字母 有大写字母

for method in [str.isalnum, str.isalpha, str.isdigit, str.islower, str.isupper]:
    print any(method(c) for c in s)

参考源

猜你喜欢

转载自blog.csdn.net/juhou/article/details/79057110