灵感来自这个关于Python缓存小整数的问题.
Python编译器是否有可能在编译时用-6替换(0 - 6)?下面的代码表明它没有.如果不可能,为什么不呢?我不认为的含义0
,-
或者6
可以在运行时不同.
如果这是可能的,为什么CPython不这样做?
# test_integers.py
def test_integers():
print "-6 is -6 ?", -6 is -6 # True
print "(0 - 6) is -6 ?", (0 - 6) is -6 # False
# import_test_integers.py
import test_integers
test_integers.test_integers()
Run Code Online (Sandbox Code Playgroud)
我的Python详细信息,以防这与实现有关:
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Run Code Online (Sandbox Code Playgroud) 我想过滤一个元组列表,比如[(1,22,1),(5,1,8),(8,3,4),(7,5,6)]
使用[1,7]
最终会给我结果的列表[(1,22,1),(5,1,8),(7,5,6)]
; 因为(8,3,4)
没有1
或者7
它被淘汰了.
我可以为此编写一个综合功能.但是如果可能的话,我正在寻找一个简短的列表理解.
谢谢.