1

I have an array of arrays:

parameters = [np.array([ 2.1e-04, -8.3e-03, 9.8e-01]), np.array([ 5.5e-04, 1.2e-01, 9.9e-01]), ...]

whose length is:

print len(parameters)

100       

If we label the elements of parameters as parameters[i][j]:

it is then possible to access each number, i.e. print parameters[1][2] gives 0.99

I also have an array:

 temperatures = [110.51, 1618.079, ...]

whose length is also 100:

print len(temperatures)

100       

Let the elements of temperatures be k:

I would like to insert each kth element of temperatures into each ith element of parameters, in order to obtain final:

final = [np.array([ 2.1e-04, -8.3e-03, 9.8e-01, 110.51]), np.array([ 5.5e-04, 1.2e-01, 9.9e-01, 1618.079]), ...]

I have tried to make something like a zip loop:

for i,j in zip(parameters, valid_temperatures): final = parameters[2][i].append(valid_temperatures[j])

but this does not work. I would appreciate if you could help me.

EDIT: Based on @hpaulj answer:

If you run Solution 1:

 parameters = [np.array([ 2.1e-04, -8.3e-03, 9.8e-01]), np.array([ 5.5e-04, 1.2e-01, 9.9e-01])]
 temperatures = [110.51, 1618.079]

     for i,(arr,t) in enumerate(zip(parameters,temperatures)):
     parameters[i] = np.append(arr,t)

 print parameters

It gives:

 [array([  2.10000000e-04,  -8.30000000e-03,   9.80000000e-01,
     1.10510000e+02]), array([  5.50000000e-04,   1.20000000e-01,   9.90000000e-01,
     1.61807900e+03])]

which is the desired output.

In addition, Solution 2:

parameters = [np.array([ 2.1e-04, -8.3e-03, 9.8e-01]), np.array([ 5.5e-04, 1.2e-01, 9.9e-01])]
temperatures = [110.51, 1618.079]

parameters = [np.append(arr,t) for arr, t in zip(parameters,temperatures)]

print parameters

also gives the desired output.

As opposed to Solution 1, Solution 2 doesn't use the ith enumerate index. Therefore, if I just split Solution 2's [np.append ... for arr ] syntax the following way:

 parameters = [np.array([ 2.1e-04, -8.3e-03, 9.8e-01]), np.array([ 5.5e-04, 1.2e-01, 9.9e-01])]
 temperatures = [110.51, 1618.079]

 for arr, t in zip(parameters,temperatures):
   parameters = np.append(arr,t)   

 print parameters

The output contains only the last iteration, and not in an "array-format":

 [  5.50000000e-04   1.20000000e-01   9.90000000e-01   1.61807900e+03]

How would it be possible to make this to work, by printing all the iterations ? Thanks

3
  • You have a list of arrays... and then a list of temperatures. Commented Dec 5, 2016 at 21:53
  • probably parameters[i] = parameters[i].append(temperatures[i]). this should append ith element of temperatures to ith element in parameters Commented Dec 5, 2016 at 21:59
  • @ArturRychlewicz No, it won't, because list != np.array Commented Dec 5, 2016 at 22:02

1 Answer 1

3

You have a list of arrays, plus another list or array:

In [656]: parameters = [np.array([1,2,3]) for _ in range(5)]
In [657]: temps=np.arange(5)

to combine them just iterate through (a list comprehension works fine for that), and perform a concatenate (array append) for each pair.

In [659]: [np.concatenate((arr,[t])) for arr, t in zip(parameters, temps)]
Out[659]: 
[array([1, 2, 3, 0]),
 array([1, 2, 3, 1]),
 array([1, 2, 3, 2]),
 array([1, 2, 3, 3]),
 array([1, 2, 3, 4])]

the use append saves us two pairs of [], otherwise it is the same:

 [np.append(arr,t) for arr, t in zip(parameters,temps)]

A clean 'in-place' version:

for i,(arr,t) in enumerate(zip(parameters,temps)):
    parameters[i] = np.append(arr,t)

================

If the subarrays are all the same length, you could turn parameters into a 2d array, and concatenate the temps:

In [663]: np.hstack((np.vstack(parameters),temps[:,None]))
Out[663]: 
array([[1, 2, 3, 0],
       [1, 2, 3, 1],
       [1, 2, 3, 2],
       [1, 2, 3, 3],
       [1, 2, 3, 4]])
Sign up to request clarification or add additional context in comments.

5 Comments

Well, you are creating new arrays anyway. It would serve to point out that this is a poor use of numpy data structures.
The inefficiency in time isn't really due to the list comprehension but the fact that np.array s are being used where probably plain Python list is the way to go.
If you are starting with a list, it is often faster to stick with list operations. Creating an array has a substantial overhead. We have to perform timings on realistic data to tell which is faster.
Right, I was responding to the other person who deleted their comment. "appending" to an array will almost certainly be slower, thought
@hpaulj Thank you very much for your three suggestions: 1) concatenate, 2) np.append (both in one line and in a for loop) and 3) hstack. Sorry for the 20h delay on the answer, but I have been digging hard into all these solutions. I have tried to understand all the syntax and how all these functions behave. First starting with enumerate, followed by zip, and then how to combine these two. I have tried to extrapolate the [np.append(arr,t) for arr, t in zip(parameters,temperatures)] solution to the clean 'in-place' solution: Please see the EDIT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.