如何在python中声明多个相似变量?

时间:2019-03-15 14:51:00

标签: python loops variables

如何声明多个(大约50个)从slider1到slider50的变量?有没有一种有效的方法,例如与for循环?

slider1 = models.IntegerField(widget=widgets.Slider, default=50, label="")
slider2 = models.IntegerField(widget=widgets.Slider, default=50, label="")
slider3 = models.IntegerField(widget=widgets.Slider, default=50, label="")
slider4 = models.IntegerField(widget=widgets.Slider, default=50, label="")
slider5 = models.IntegerField(widget=widgets.Slider, default=50, label="")
slider6 = models.IntegerField(widget=widgets.Slider, default=50, label="")
slider7 = models.IntegerField(widget=widgets.Slider, default=50, label="")
slider8 = models.IntegerField(widget=widgets.Slider, default=50, label="")
slider9 = models.IntegerField(widget=widgets.Slider, default=50, label="")
slider10 = models.IntegerField(widget=widgets.Slider, default=50, label="")

1 个答案:

答案 0 :(得分:-1)

我建议为此使用字典:

d = {}

for x in range(1,10):
        d["slider{0}".format(x)]= models.IntegerField(widget=widgets.Slider, default=50, label="")