2

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!

rh1990
  • 880
  • 7
  • 17
  • 32
  • 5
    why don't you use dictionaries? So you can have "separate" 1D arrays that you can access by name (key) – FLab Apr 04 '17 at 10:51
  • I've heard of `pandas` if that's useful for this? – rh1990 Apr 04 '17 at 10:53
  • 1
    as an aside, note that `range(3)` returns `[0, 1, 2]` – asongtoruin Apr 04 '17 at 11:08
  • 1
    @RichardHall It certainly could be, but it's difficult to say without more information. In this case, a `pandas.DataFrame` with 3 columns should work. Also, for performance reasons, you should try to avoid iterating over large arrays in Python; use vectorised functions, such as those provided by `pandas` and `numpy`, where possible. – Will Vousden Apr 04 '17 at 11:12
  • @erip apologies, had my 2.7 head on. The point I was trying to make is that in the pseudocode there would be an attempt to access `data_0` and no attempt to access `data_3` due to the values accessed by `range`, regardless of Python version. Your point stands though. – asongtoruin Apr 04 '17 at 11:17
  • @erip How do you know the OP is using Python 3? – Will Vousden Apr 04 '17 at 12:21
  • Should have said, I am using 2.7 :) – rh1990 Apr 04 '17 at 15:04

4 Answers4

3

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]
Community
  • 1
  • 1
Abhimanyu singh
  • 397
  • 3
  • 9
  • 3
    I'm wary of upvoting this, because while it does answer the question, it's almost certainly the wrong solution to the OP's problem. I think your answer should make this clear, and provide an example of how to do it correctly using dictionaries. – Will Vousden Apr 04 '17 at 11:02
  • 1
    I Agree . Have edited to add the Dictionary based solution – Abhimanyu singh Apr 04 '17 at 11:06
  • 1
    You can create the data_dict using code also . `data_dict = {'data_'+str(i) :[i]*l for i in range(n)}` – Abhimanyu singh Apr 04 '17 at 11:21
0

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.

Neuneck
  • 294
  • 1
  • 3
  • 14
0

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 ;)

Carlo 1585
  • 1,455
  • 1
  • 22
  • 40
0

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]
Community
  • 1
  • 1
Qrom
  • 487
  • 5
  • 20