Python 如何将变量从.py文件导入列表,以便使用该列表上的随机模块?

Python 如何将变量从.py文件导入列表,以便使用该列表上的随机模块?,python,python-3.x,list,Python,Python 3.x,List,我有一个python文件,其中包含大约60行变量,这些变量是文件导入(用于pygame)。这是imports.py文件,它只包含变量,没有其他内容。如何将它们导入mymain.py中的列表 导入.py import pygame pygame.init() one_surf = pygame.image.load("*") two_surf = pygame.image.load("*") three_surf = pygame.image.load

我有一个python文件,其中包含大约60行变量,这些变量是文件导入(用于pygame)。这是
imports.py
文件,它只包含变量,没有其他内容。如何将它们导入my
main.py
中的列表

导入.py

import pygame

pygame.init()
one_surf = pygame.image.load("*")

two_surf = pygame.image.load("*")

three_surf = pygame.image.load("*")

four_surf = pygame.image.load("*")

five_surf = pygame.image.load("*")
.
.
.

也许可以像这样稍微重新排列代码:

imports.py

导入pygame
pygame.init()
冲浪=[
pygame.image.load(“*”),
pygame.image.load(“*”),
pygame.image.load(“*”),
pygame.image.load(“*”),
pygame.image.load(“*”),
... ]
something\u other.py

从导入导入冲浪

然后,您可以使用
surf[2]
访问以前称为
three\u-surf
的内容,使用字典将提供与将变量放入列表相同的功能,因为您可以根据名称/值引用每个图像

import pygame
pygame.init()

surf = {
    'one_surf': pygame.image.load("*"),
    'two_surf': pygame.image.load("*"),
    'three_surf': pygame.image.load("*"),
    'four_surf': pygame.image.load("*"),
    'five_surf': pygame.image.load("*")
}

将信息存储在字典中意味着您可以根据一个键引用每个图像:
surf['one_-surf']
与前面的
one_-surf
变量保持相同的值。

为什么不在导入前将所有
imports.py
变量放入列表?@MZ然后导入列表?听起来是个好主意