一次将变量分配给一堆变量
Assigning a variable amount to a bunch of variables at once
有没有更好的方法可以同时为一堆其他变量声明一个 var 值。我正在尝试向下面的所有其他变量声明 Int_amount
。我正在尝试最小化代码,也许 for 循环是声明所有值的更好选择。
Int_amount = 100
Amount = Int_amount
S_Amount = Int_amount
L_Amount = Int_amount
NonC_Amount = Int_amount
NonC_S_Amount = Int_amount
NonC_L_Amount = Int_amount
Amount, S_Amount, L_Amount, NonC_Amount, NonC_S_Amount, NonC_L_Amount = [Int_amount]*6
以下是使用 for 循环的方法。
my_var = [
'S_Amount', 'L_Amount',
'NonC_Amount', 'NonC_S Amount',
'NonC_L_Amount']
for i in my_var:
globals()[i] = 100
有没有更好的方法可以同时为一堆其他变量声明一个 var 值。我正在尝试向下面的所有其他变量声明 Int_amount
。我正在尝试最小化代码,也许 for 循环是声明所有值的更好选择。
Int_amount = 100
Amount = Int_amount
S_Amount = Int_amount
L_Amount = Int_amount
NonC_Amount = Int_amount
NonC_S_Amount = Int_amount
NonC_L_Amount = Int_amount
Amount, S_Amount, L_Amount, NonC_Amount, NonC_S_Amount, NonC_L_Amount = [Int_amount]*6
以下是使用 for 循环的方法。
my_var = [
'S_Amount', 'L_Amount',
'NonC_Amount', 'NonC_S Amount',
'NonC_L_Amount']
for i in my_var:
globals()[i] = 100