a1 = 0
a2 = 1
x = [] #here I have declared an empty list
for i in range(2):
x.append('a'+str(i+1)) #to append the variable with a numbering scheme
print (x)
a1 = 0
a2 = 1
x = [] #here I have declared an empty list
for i in range(2):
x.append(locals()['a'+str(i+1)]) #to append the variable with a numbering scheme
print (x)
d = {'a1': 0, 'a2':1}
x = [] #here I have declared an empty list
for i in range(2):
x.append(d['a'+str(i+1)]) #to append the variable with a numbering scheme
print (x)
如果必须使用作用域中已有的变量,则可以使用
locals()
将所有局部变量作为dict获取为此使用词典:
相关问题 更多 >
编程相关推荐