Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3如果不是条件简化_Python_Python 3.x_If Statement - Fatal编程技术网

Python3如果不是条件简化

Python3如果不是条件简化,python,python-3.x,if-statement,Python,Python 3.x,If Statement,这两个条件检查是否相同?我想不出如何检查它们是否相同 l1 = [] l2 = [] if not l1 and not l2: print ('y') if not (l1 and l2): print ('y') 感谢所有回复的人,我已经做了一些基本的计时,看看哪个更快 import time l1 = [] l2 = [] st = time.time() for i in range(100000000): if not l1 and not l2:

这两个条件检查是否相同?我想不出如何检查它们是否相同

l1 = []
l2 = []

if not l1 and not l2:
    print ('y')

if not (l1 and l2):
    print ('y')
感谢所有回复的人,我已经做了一些基本的计时,看看哪个更快

import time
l1 = []
l2 = []

st = time.time()
for i in range(100000000):
    if not l1 and not l2:
        pass
end = time.time()
print ('if not l1 and not l2: '+str(end-st))

st = time.time()
for i in range(100000000):
    if not (l1 or l2):
        pass
end = time.time()
print ('if not (l1 or l2): '+str(end-st))
印刷品:

if not l1 and not l2: 8.533874750137329
if not (l1 or l2): 7.91820216178894

不,它们不一样。看

反例是:

l1 = [0]
l2 = []

它们不一样。您需要修改第二个条件,如下所示,使其等效:

l1 = []
l2 = []

if not l1 and not l2:
    print ('y')

if not (l1 or l2):
    print ('y')
if not (l1 or l2):
    print ('y')

如果要保持相同,请使用或操作:

l1 = []
l2 = []

if not l1 and not l2:
    print ('y')
等价物:

l1 = []
l2 = []

if not l1 and not l2:
    print ('y')

if not (l1 or l2):
    print ('y')
if not (l1 or l2):
    print ('y')