rh1990
rh1990

Reputation: 940

Python iterate through variables based off of their names

I have some arrays of data that I'd like to perform similar calculations on so I want to iterate through them based off of their names. Some pseudo code for example:

l = 1000 # example length of data arrays
n = 3 # number of arrays
data_1 = [1] * l
data_2 = [2] * l
data_3 = [3] * l

# l can vary in size, so arrays aren't fixed length

for i in range(n): 
    for j in range(l):
        data_[i][j] = some_length_based_maths[j]

It's the syntax of calling the data arrays by their name in an iterative way that's throwing me off.

I probably should use a 2D array or something and call the elements [i][j], but it's easier to keep it all separate in this application if possible.

Thanks!

Upvotes: 3

Views: 7005

Answers (4)

Qrom
Qrom

Reputation: 497

If you want to access variables based on their names, you can use the alternative solutions of vars(), globals() or locals() depending on your context. You can learn more about the differences here.

Your loop should then look like this :

for i in range(n): 
    for j in range(l):
        vars()['data_'+str(i)][j] = some_length_based_maths[j]

Upvotes: 0

Abhimanyu singh
Abhimanyu singh

Reputation: 417

You will be better off using Python Dictionaries for a cleaner solution . For your current requirement you might do something like this:

for i in range(n): 
    for j in range(l):
        eval('data_'+str(i))[j] = some_length_based_maths[j]

And sort of disclaimer : Is using eval in Python a bad practice?

A better way to handle this (using Dictionaries)

l = 1000 # example length of data arrays
data_dict = {}
data_dict['data_1'] = [1] * l
data_dict['data_2'] = [2] * l
data_dict['data_3'] = [3] * l

# l can vary in size, so arrays aren't fixed length

for i in data_dict: 
    for j in range(l):
        data_dict[i][j] = some_length_based_maths[j]

Upvotes: 4

Carlo 1585
Carlo 1585

Reputation: 1477

Maybe this is not the best solution but you can try something like that:

def some_length_based_maths(array):
    result=[]
    i=0
    while i<len(array):
        do_something=.....
        result.append(do_something)
        i+=1
    return (result)


data_1 = [.....]
data_2 = [.....]
data_3 = [.....]


i=0
while i<3:
    if i==0:
        array_result1=some_length_based_maths(data_1)
    elif i==1:
        array_result2=some_length_based_maths(data_2)
    else:
        array_result3=some_length_based_maths(data_3)
    i+=1

You have your 3 array, for each of them you call the function "some_length_based_maths", for each element of the array you do some calculation and you introduce the result in another array that you give back to the main.

it's just an example that could help you, but if you give me more details it can be structured in a slide different way ;)

Upvotes: 0

Neuneck
Neuneck

Reputation: 334

You can iterate over a number of lists by creating a list of lists:

for list in [list_1, list_2, list_3]:
    # operate on local variable `list`

Granted, this is cumbersome if you have a large number of lists, but then you should consider creating a list/dictionary instad of individual variables from the start.

Upvotes: 0

Related Questions