2

I have a function that returns a list. I think I use np.append to add this list as a new line in an array, my intention is as follow:

list = 4 5 6
b = 1 2 3
b = np.append(b, list)

output;

1 2 3
4 5 6

This isn't the code I use (there's a lot of messing around in between). But the output I get is this:

2016年06月01日 PRINT [ 99.86 99.928 99.9 99.875 99.8 89.7933
 97.60018333 98.903 99.928 0.2801201 98.95 98.93
 98.87 98.94 99.05 89.097 97.6712 98.87
 99.59 0.23538903 99.711 99.732 99.725 99.724
 99.769 89.777 98.12053333 99.68 99.88
 0.30333219 99.805 99.79 99.743 99.71 99.69
 89.7728 98.06653333 99.617 99.82 0.28981292
 99.882 99.879 99.865 99.84 99.9 89.9206
 98.29823333 99.82 100.08 0.31420778]

Is this a 10 column by 5 row array/matrix or is this a 50 column/row array? I feel like I'm missing something here - or is it just that the output doesn't really show the shape of the array?

Billal BEGUERADJ
23k45 gold badges125 silver badges142 bronze badges
asked Jul 30, 2016 at 0:29
2
  • Why not get the shape of the array from numpy, eg. b.shape Ref: numpy.ndarray.shape Commented Jul 30, 2016 at 0:46
  • I see only one set of [] - so it's 1d, a 1d 50 element array. Like your shorted 6 element array. Commented Jul 30, 2016 at 3:35

2 Answers 2

3

True list append:

In [701]: alist = [4,5,6]
In [702]: b=[1,2,3]
In [703]: b.append(alist)
In [704]: b
Out[704]: [1, 2, 3, [4, 5, 6]]

bad array operation:

In [705]: anArray=np.array([4,5,6])
In [706]: b=np.array([1,2,3])
In [707]: b=np.append(b,anArray)
In [708]: b
Out[708]: array([1, 2, 3, 4, 5, 6])
In [709]: b.shape
Out[709]: (6,)

Here I just concatenated anArray onto b, making a longer array.

I've said this before - np.append is not a good function. It looks too much like the list append, and people end up misusing it. Either they miss the fact that it returns a new array, as opposed to modifying in-place. Or they use it repeatedly.

Here's the preferred way of collecting lists or arrays and joining them into one

In [710]: alist = []
In [711]: b=np.array([1,2,3]) # could be b=[1,2,3]
In [712]: alist.append(b)
In [713]: b=np.array([4,5,6]) # b=[4,5,6]
In [714]: alist.append(b)
In [715]: alist
Out[715]: [array([1, 2, 3]), array([4, 5, 6])]
In [716]: np.array(alist)
Out[716]: 
array([[1, 2, 3],
 [4, 5, 6]])
In [717]: _.shape
Out[717]: (2, 3)

The result is a 2d array. List append is much faster than array append (which is real array concatenate). Build the list and then make the array.

The most common way of defining a 2d array is with a list of lists:

In [718]: np.array([[1,2,3],[4,5,6]])
Out[718]: 
array([[1, 2, 3],
 [4, 5, 6]])

np.concatenate is another option for joining arrays and lists. If gives more control over how they are joined, but you have to pay attention to the dimensions of the inputs (you should pay attention to those anyways).

There are several 'stack' functions which streamline the dimension handling a bit, stack, hstack, vstack and yes, append. It's worth looking at their code.

answered Jul 30, 2016 at 1:00
Sign up to request clarification or add additional context in comments.

1 Comment

I think you said np.array in a spot where you meant np.append.
1

you should use hstack or vstack

import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.vstack((a,b))

gives

array([[1, 2, 3],
 [4, 5, 6]])

or

np.hstack((a,b))

gives

array([1, 2, 3, 4, 5, 6])
answered Jul 30, 2016 at 1:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.