我试图做一些简单的事情,如下面的
for k in range(0,2)
outsetk = Reader(FileName='/dir/outset-'+str(k)+'.q')
生成以下
outset0 = Reader(FileName='/dir/outset-'+str(0)+'.q')
outset1 = Reader(FileName='/dir/outset-'+str(1)+'.q')
outset2 = Reader(FileName='/dir/outset-'+str(2)+'.q')
其中Reader是一些只有一个输入的预定义函数。我知道作业的右侧是正确的,但我不确定如何做左侧。
答案 0 :(得分:1)
尝试使用字典来保存结果。像这样:
outsets = {}
for k in range(0, 3):
outsets[k] = Reader(FileName='/dir/outset-' + str(k) + '.q')
然后你会像这样访问outset0
:
outsets[0] # equivalent to your outset0
你也可以这样做,以获得你的例子中提到的相同名称:
outsets = {}
name = 'outset{}'
for k in range(0, 3):
outsets[name.format(k)] = Reader(FileName='/dir/outset-' + str(k) + '.q')
要访问outset0
,您需要使用outsets['outset0']
如果您想使用列表,请尝试以下方法:
outsets = []
for k in range(0, 3):
outsets.append(Reader(FileName='/dir/outset-' + str(k) + '.q')
然后你会以同样的方式访问outset0
:
outsets[0] # equivalent to your outset0