美文网首页
python对方法的迭代

python对方法的迭代

作者: greedyhao | 来源:发表于2017-08-23 16:28 被阅读0次

    欢迎关注我的博客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)
    

    参考源

    相关文章

      网友评论

          本文标题:python对方法的迭代

          本文链接:https://www.haomeiwen.com/subject/gtxjdxtx.html