0

I'm new to Python, but I use it to process data. I've got a large amount of data stored as float arrays:

data1 data2 data3 

I want to run similar processing for each data file. I was thinking of using a for loop:

for i in range(1,4): 

I would like to then multiply the three data files by two, but I'm not sure how to continue afterwards. I imagine it would look like his:

for i in range(1,4): data_i=data_i*2 

Thank you.

3
  • call the three data files?!?!?! Commented Oct 3, 2014 at 12:04
  • For example, I would like to multiply all the data files by 2. So data1*2, data2*2, data3*2. Commented Oct 3, 2014 at 12:08
  • If you want to change the variable names it is impossible, at least without altering the python processor Commented Oct 3, 2014 at 12:46

3 Answers 3

1

You could make a two-dimensional array, meaning you put your float arrays inside another array.

Your situation right now would look like this:

data1 = [12, 2, 5] data2 = [2, 4, 8] data3 = [3, 0, 1] 

By putting your arrays inside another array by doing this:

datax = [data1, data2, data3] 

Your new situation would look like this:

datax = [[12, 2, 5], [2, 4, 8], [3, 0, 1]] 

Now we can loop over the new datax array and perform an action on it's elements, data1, data2 and data3.

Something along the lines of:

datax = [[12, 2, 5], [2, 4, 8], [3, 0, 1]] for sub_array in datax: perform_action(sub_array) 
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply store the data in e.g. list

data_list = [data1, data2, data3] for i, data in enumerate(data_list): some_fancy_stuff data_list[i] = data * 2 

Some explanation - enumerate will literally enumerate the items of the list with index i and also assigns data_list[i] to variable data. Than you can do whatever you want with data and its index i.

Comments

0

You could also use Python comprehension instead of loops, here is an example to illustrate it:

>>> a1 = [1,2,3] >>> a2 = [4,5,6] >>> a3 = [7,8,9] >>> A = [a1, a2, a3] >>> >>> print [[x*2 for x in a] for a in A] [[2, 4, 6], [8, 10, 12], [14, 16, 18]] >>> 

Let me explain it also.

The following construction is called comprehension:

a = [x*2 for x in X] 

It produces an array (as you could see the brackets [ ]) of processed values (in the example value multiplication by two) from array X. It's as if we wrote:

a = [] for x in X: a.append(x*2) 

but in more concise way. In your situation we used two comprehension one in one:

[x*2 for x in a] 

and:

[ [....] for a in A] 

So it's the same as if we did:

result = [] for a in A: for x in a: result.append(x*2) 

but in more concise way again.

 

If you asked about variable names modification, so it is not possible in the Python without altering the language processor. But I guess you don't need it in that task at all.

1 Comment

Only use map or a list comprehension if you actually need the resulting list. If you are simply using it for the side effect of calling some_func on each element of A, use an explicit for loop instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.