Python:简化变量声明(使用for循环?)

Python:简化变量声明(使用for循环?),python,for-loop,variables,declaration,Python,For Loop,Variables,Declaration,我是python新手,我在代码中声明了一些变量,但它们几乎是相似的,我想知道是否可以使用for循环或类似的方法来简化这一过程,从而不需要10行声明 Left_10 = PhotoImage(file = 'image_10.jpg') Left_9 = PhotoImage(file = 'image_9.jpg') Left_8 = PhotoImage(file = 'image_8.jpg') Left_7 = PhotoImage(file = 'image_7.jpg') Left_6

我是python新手,我在代码中声明了一些变量,但它们几乎是相似的,我想知道是否可以使用
for
循环或类似的方法来简化这一过程,从而不需要10行声明

Left_10 = PhotoImage(file = 'image_10.jpg')
Left_9 = PhotoImage(file = 'image_9.jpg')
Left_8 = PhotoImage(file = 'image_8.jpg')
Left_7 = PhotoImage(file = 'image_7.jpg')
Left_6 = PhotoImage(file = 'image_6.jpg')
Left_5 = PhotoImage(file = 'image_5.jpg')
Left_4 = PhotoImage(file = 'image_4.jpg')
Left_3 = PhotoImage(file = 'image_3.jpg')
Left_2 = PhotoImage(file = 'image_2.jpg')
Left_1 = PhotoImage(file = 'image_1.jpg')
感谢您的帮助

使用口述:

Left = {i: PhotoImage(file = 'image_'+str(i)+'.jpg') for i in range(1,11)}

使用
左[7]

这基本上是@Julien所说的内容的副本,但作为
列表而不是
目录

Left=[PhotoImage(file=f“image_{i}.jpg”)用于范围(1,11)内的i]
您可以通过索引访问它们并对它们进行排序(不同于dict
):
Left[0]#PhotoImage with file=image_1.jpg

不要这样做。使用适当的数据结构。检查这是否回答了您的问题?