我试图创建和分配10个变量,仅由它们的索引区分,所有这些变量都是qazxsw poi循环中的空列表。
理想的输出是有for
我知道我可以写出这一切,但我认为我应该能够创建一个简单的循环。主要问题是在每次迭代时分配空列表
agent_1 = [], agent_2 = [], agent_n = []
为什么不使用等于agent_i的键的dict对象。
for i in range(1,10):
agent_ + i = []
这是dic = {}
for i in range(1,10):
dic["agent_" + str(i)] = []
// access the dic directly and iterating purpose also just iterate through the dictionary.
print dic["agent_1"]
# iteration over the dictionary
for key,value in dic.items():
print key,value
的链接
这是一个可怕的想法。我会让代码说明一切:
code snippet
n = 10
for i in range(n):
globals()['agent_%d' % (i + 1)] = []