Python 如何在每次打印时评估条件列表?
我想知道在每次打印条件列表时,是否有必要评估条件列表 例如:Python 如何在每次打印时评估条件列表?,python,python-3.x,python-2.7,list,updates,Python,Python 3.x,Python 2.7,List,Updates,我想知道在每次打印条件列表时,是否有必要评估条件列表 例如: a,b,c,d=10,15,30,56 li=[a%10==0, b<5, c//3==10, d%2==0] print(li) b=3 print(li) a、b、c、d=10,15,30,56 li=[a%10==0,b最好的方法可能是使用函数: def evaluate(a, b, c, d): return [a%10==0, b<5, c//3==10, d%2==0] print(evaluate
a,b,c,d=10,15,30,56
li=[a%10==0, b<5, c//3==10, d%2==0]
print(li)
b=3
print(li)
a、b、c、d=10,15,30,56
li=[a%10==0,b最好的方法可能是使用函数:
def evaluate(a, b, c, d):
return [a%10==0, b<5, c//3==10, d%2==0]
print(evaluate(a, b, c, d))
a, b, c, d = 10, 15, 30, 56
def get_list(a,b,c,d):
return [a%10 == 0 , b < 5, c//3 == 10, d%2 == 0]
print(get_list(a,b,c,d))
b=3
print(get_list(a,b,c,d))
def评估(a、b、c、d):
return[a%10==0,b它的可能,它调用了一个函数:
def evaluate(a, b, c, d):
return [a%10==0, b<5, c//3==10, d%2==0]
print(evaluate(a, b, c, d))
a, b, c, d = 10, 15, 30, 56
def get_list(a,b,c,d):
return [a%10 == 0 , b < 5, c//3 == 10, d%2 == 0]
print(get_list(a,b,c,d))
b=3
print(get_list(a,b,c,d))
这也是可能的-它们在执行时是惰性绑定的-因此反映了对b
的更改:
k = lambda: [a%10 == 0 , b < 5, c//3 == 10, d%2 == 0]
print(k()) # execute the lambda
b=3
print(k()) # execute again
使用函数update@PatrickArtner,没有偏执狂也行。@Austin它会吗?很高兴知道-而且它更便宜,没有元组construction@PatrickArtner,仍然存在元组结构。解释器将把逗号分隔的值列表解释为元组,然后将其解包到变量中。@PatrickArtner Same tuple co指令。元组由逗号分隔的序列定义。除空元组外,括号是可选的,除非在特定上下文中需要。此处不需要括号,因为赋值“=”的绑定比逗号更松散。a,b=1,2
如果解析为a,(b=1),2
,则毫无意义。