• python 空字符串是其他任何字符的子集


    >>> '' in 'hello'
    True
    >>> [] in ['hello','world',123]
    False
    

      起初不明白为啥一个是True,一个是False,查阅各种资料,明白了:空字符串永远被视作是其他任何字符串的子集,因此"" in "abc"将返回True。

      首先,需要明白in和not in 的用法解释如下:

    翻译如下:

      对容器类型,例如list、tuple、set、frozenset、dict或collections.deque,表达式x in y等价于any(x is e or x == e for e in y)。

        (如果y中有元素e满足x is e或x == e,那么x in y就是True,否则x in y就是False。)

      对字符串和bytes类型,x in y为真当且仅当x是y的子串。等价测试为y.find(x) != -1。空字符串永远被视作是其他任何字符串的子集,因此"" in "abc"将返回True

      对列表类型,列表[1, 2, 3, 4]中有4个元素1、2、3和4,没有元素e能满足e is [3]或e == [3],因此[3] in [1, 2, 3, 4]为False。

    扩展:

      find函数原型:find(str, pos_start, pos_end)

    • str:被查找“字串”
    • pos_start:查找的首字母位置(从0开始计数。默认:0)
    • pos_end: 查找的末尾位置(默认-1)
     1.str = "0123"
        print str.find("2",1,-1)      #2
    
     2.str = "1111"
        print str.find("1")           #0,首次出现的位置
    

      

      

  • 相关阅读:
    Codeforces 601B. Lipshitz Sequence(单调栈)
    C++11正则表达式初探
    Codeforces 1051 D.Bicolorings(DP)
    数据库规范——学习小记
    2016 NEERC, Moscow Subregional Contest K. Knights of the Old Republic(Kruskal思想)
    10.2路径
    10.1jihe
    8/9三角形
    8/9,集合的运算
    6.2收费
  • 原文地址:https://www.cnblogs.com/xw1314/p/11711842.html
Copyright © 2020-2023  润新知