gpt4 book ai didi

python - 命名列表列表

转载 作者:太空宇宙 更新时间:2023-11-04 09:07:50 25 4
gpt4 key购买 nike

我需要在 python 脚本中创建一个命名列表列表。

我想要做的是创建一个 mklist 方法,该方法将获取列表中的字符串并创建为每个字符串命名的列表。所以,从这里开始:

a = "area"

for i in range(1, 37):
x = str(a) + str("%02d" % (i,))
' '.join(x.split())

我想得到以下信息:

area01 = []
area02 = []
area03 = []
area04 = []
area05 = []
area06 = []
area07 = []
area08 = []
area09 = []
area10 = []
area11 = []
area12 = []
area13 = []
area14 = []
area15 = []
area16 = []
area17 = []
area18 = []
area19 = []
area20 = []
area21 = []
area22 = []
area23 = []
area24 = []
area25 = []
area26 = []
area27 = []
area28 = []
area29 = []
area30 = []
area31 = []
area32 = []
area33 = []
area34 = []
area35 = []
area36 = []

有什么建议吗?我似乎无法得到它。谢谢!E

最佳答案

这需要一个列表列表:

area = [[] for i in range(37)]

或者列表字典:

area = {i: [] for i in range(1, 37)}        # Python 2.7+
area = dict((i, []) for i in range(1, 37)) # Python 2.6 or earlier

然后您可以访问每个项目:

area[1]
area[2]
...
area[36]

关于python - 命名列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18362343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com