Python’s peephole optimizer now recognizes patterns such x in {1, 2, 3}
as being a test for membership in a set of constants. The optimizer
recasts the set as a frozenset and stores the pre-built constant.
Now that the speed penalty is gone, it is practical to start writing
membership tests using set-notation.
您可以像这样使用
in
运算符:对于Python 3.2+:
^{pr2}$从docs:
如果检查比集合中的成员身份更复杂,并且希望减少代码行数,可以使用conditional expressions:
您可以使用^{} 来缩短代码:
或者,如果
^{pr2}$x
是单个字符:相关问题 更多 >
编程相关推荐