假设我有多个名为data_w1
、data_w2
、data_w3
、data_wn
的列表。我有一个接受整数band
作为输入的函数,我希望该函数只对相应的列表进行操作。你知道吗
我很熟悉字符串替换,但我这里不处理字符串,所以我该怎么做呢?一些伪代码:
def my_function(band):
wband_new = []
for entry in data_wband:
# do stuff
wband_new.append( #new stuff )
return wband_new
但是执行上述操作并没有像预期的那样起作用,因为我得到的错误是没有定义任何带有wband
的内容。我该怎么做?你知道吗
假设在函数前面的脚本中有
data
变量。您需要做的是用globals()['data_w'+str(band)]
替换data_wband
:不完全确定你在问什么,但是如果你想要列表1,2,…,n,那么一个整数i,你想要得到第i个列表,只需要有一个列表列表,并用整数i索引外部列表(在你的例子中称为band)。你知道吗
相关问题 更多 >
编程相关推荐