2017-04-13 40 views
5

很奇怪地看到使用“在乙方对str中”语法的一段代码,如:Python字符串在...语法

>>> 'test' in 'testtest' in 'testtesttest' 
True 
>>> 'test' in 'testtest' in 'tb3' 
False 
>>> 'test' in 'testtesta' in 'testtesttest' 
False 
>>> 'test' in ('testtest' in 'testtesttest') 
Traceback (most recent call last): 
    File "<input>", line 1, in <module> 
    'test' in ('testtest' in 'testtesttest') 
TypeError: argument of type 'bool' is not iterable 

看来“在......在.. ''与'< ... < ...'比较类似。但一个快速谷歌并没有引导我到正式的答案。任何帮助?从Python documentation

+0

我想你的猜测是正确的。 “C中B中的A”表示“A中B和B中C”,就像“a Organis

回答

5

官方的回答:

comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!=" 
        | "is" ["not"] | ["not"] "in" 

in关键字是比较操作。和“比较可以任意链接”。请注意,这不限于“值比较”(>==等)。

有问题的代码检查每个链是否是链中下一项的子串。