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')