我找不到能帮助我解决这个问题的指南。所以我希望有人可以帮我解释一下Python中的这种编程。我正在尝试编写类似这样的代码:
def Runner():
for G in range(someRange):
makeListObjectcalled 'ListNumber'+'G'
ListNumberg.append(G*500000 or whatever)
print ListNumberG
#so I would have a someRange amount of lists
#named 0,1,2,3...(up to someRange) I could look through
我认为可以用课程来完成(实际上我猜这是他们的目的......)但我不确定。有人可以给我一些澄清吗?
答案 0 :(得分:1)
您希望动态创建存储值数组的类型列表变量。
一种更容易和更好的方法(比杂耍未知的变量名称)是使用字典来保存您的列表,因此您可以按名称/键查找它们:
(伪代码,没有我的Python解释器)
# create a dictionary to store your ListNumberG's
dict_of_lists = {}
# down the line in your loop, add each generated list to the dict:
dict_of_lists['ListNumberG'] = ListNumberG
稍后您可以通过
按名称/键找到列表print(dict_of_lists['ListNumberG'])
或循环播放
for idx in range(bestguess):
print(dict_of_lists['ListNumber%s' % (idx,)])
答案 1 :(得分:1)
看起来你真正想要的是列表清单。
def Runner():
Lists = []
for G in range(someRange):
Lists[G] = []
Lists[G].append(G*500000 or whatever)
print Lists[G]
#This way, you have Lists[0], Lists[1], ..., Lists[someRange]