-1

Say I have multiple lists called data_w1, data_w2, data_w3, ..., data_wn. I have a function that takes an integer band as an input, and I'd like the function to operate only on the corresponding list.

I am familiar with string substitution, but I'm not dealing with strings here, so how would I do this? Some pseudocode:

def my_function(band):
    wband_new = []
    for entry in data_wband:
        # do stuff
        wband_new.append( #new stuff )
    return wband_new

But doing the above doesn't work as expected because I get the errors that anything with wband in it isn't defined. How can I do this?

curious_cosmo
  • 1,184
  • 1
  • 18
  • 36

2 Answers2

0

Suppose you have your data variables in the script before the function. What you need to do is substitute data_wband with globals()['data_w'+str(band)]:

data_w1 = [1,2,3]
data_w2 = [4,5,6]

def my_function(band):
    wband_new = []
    for entry in globals()['data_w'+str(band)]:
        # do stuff
        wband_new.append( #new stuff )
    return wband_new
Bruno Lubascher
  • 2,071
  • 14
  • 19
0

Not exactly sure what you're asking, but if you mean to have lists 1, 2, ..., n then an integer i and you want to get the i'th list, simply have a list of lists and index the outer list with the integer i (in your case called band).

l = [data_w1, data_w2, data_w3]
list_to_operate_on = l[band]
func(list_to_operate_on)