2

I have 8 elements and I want to add them into a array in numpy. I used np.append() but it seems that I can only add two elements at one time. I want to add all 8 elements at once. first_1 =35.72438966508524, first_2 = 35.73839550991734, etc.

35.72438966508524 35.73839550991734 35.81944190992304 
35.80549149559467 35.78399019604507 36.03781192909738 
35.9957696566448 35.94692998938782

np.append(first_1,first_2,first_3,first_4,first_5,first_6,first_7,first_8)

The error is

TypeError: append() takes from 2 to 3 positional arguments but 8 were given
6
  • 1
    You probably want np.concatenate, but please format your question so it's at least somewhat readable. Commented Aug 15, 2018 at 21:38
  • I used it. But the error is concatenate() takes at most 3 arguments (8 given). Is there any other way I can add all of them? Commented Aug 15, 2018 at 21:41
  • 2
    You're probably calling it wrong. Use np.concatenate([a,b,c,d,e,f,g...]). Don't pass each array as an argument, pass a list of the arguments. In your case it would be np.concatenate([first_1,first_2,first_3,first_4,first_5,first_6,first_7,first_8]) Commented Aug 15, 2018 at 21:41
  • the code is np.concatenate([first_1,first_2,first_3,first_4,first_5,first_6,first_7,first_8]). Still got an error: ValueError: zero-dimensional arrays cannot be concatenated Commented Aug 15, 2018 at 21:44
  • 1
    Then please provide an minimal reproducible example, as you have not shown enough information to solve your problem. Commented Aug 15, 2018 at 21:44

3 Answers 3

4
first = 35.72438966508524 
second = 35.73839550991734 
third = 35.81944190992304
forth = 35.80549149559467 
fifth = 35.78399019604507 
sixth = 36.03781192909738 
seventh = 35.9957696566448 
eighth = 35.94692998938782

now to make a new numpy array:

a = np.array([first, second, third, forth, fifth, sixth, seventh, eighth])

output:

a
Out[89]: 
array([35.72438967, 35.73839551, 35.81944191, 35.8054915 , 35.7839902 ,
       36.03781193, 35.99576966, 35.94692999])

to append to existing array (using previously created 'a'):

a = np.append(a, [first, second, third, forth, fifth, sixth, seventh, eight], axis=0)

which gives:

a
Out[93]: 
array([35.72438967, 35.73839551, 35.81944191, 35.8054915 , 35.7839902 ,
       36.03781193, 35.99576966, 35.94692999, 35.72438967, 35.73839551,
       35.81944191, 35.8054915 , 35.7839902 , 36.03781193, 35.99576966,
       35.94692999])
Sign up to request clarification or add additional context in comments.

Comments

3

Correct syntax is (if I suppose that you wanted to append your 8 values to an numpy array named as ar:

np.append(ar, (first_1, first_2, first_3, first_4, first_5, first_6, first_7, first_8))
  • The first argument is your original numpy array,
  • The second one is the tuple (or the list, or other array-like object) of your values, so those values have to be in the parentheses.

Comments

1
np.array([first_1,first_2,first_3,first_4,first_5,first_6,first_7,first_8])

4 Comments

TypeError: append() missing 1 required positional argument: 'values'. What does it mean?
@user3483203 first_1... aren't arrays, they are numbers lol
Well this was a terribly unclear question, my mistake :)
@user3483203 ahaha no problem, I did a double take as well, she worded it pretty poorly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.