python可以自己创建变量吗?我必须做一个需要太多变量的程序。它能做到吗?它是自我吗?例子:
`x= 10
#and here i need to make for example x1 but i dont want to type it
x1 = 10
嗨,有很多选项,但就像chepner说的,每一个都比写变量名要大。
如果你只有数字,你可以使用列表:
x = [10,11,12,13,14]
则变量为x[0] x[1] x[2] ...
如果你想用一个名字来标识每个变量,你可以使用字典。假设您想创建10个名称为x1,x2,... x10
my_dict = {}
for i in range(10):
my_dict[f'x{i}'] = 10
然后通过my_dict['x1'] , my_dict['x2']
访问