-3

in this code im trying to add the sum of values and add it to an array named array_values, but it didnt, only prints []

array_values = ([])

value = 0.0
for a in range(0, 8):
    for b in range (1, 5):
        value = value + float(klines[a][b])
        #print(value)
    np.append(array_values, value)#FIX array_values.append(value)
    print("añadiendo: ",value)
    value = 0.0

print(array_values)
Cr2
  • 11
  • 2
  • Did you already read https://numpy.org/doc/stable/reference/generated/numpy.append.html? – mkrieger1 Aug 20 '21 at 18:24
  • 1
    This should answer your question: [python numpy array append not working in .py file, but works in terminal](https://stackoverflow.com/questions/37507247/python-numpy-array-append-not-working-in-py-file-but-works-in-terminal) – mkrieger1 Aug 20 '21 at 18:28

2 Answers2

0

Does this solve your problem?

import numpy as np

array_values = ([])

value = 0.0
for a in range(0, 8):
    for b in range (1, 5):
        value = value + float(klines[a][b])
        #print(value)
    array_values = np.append(array_values, value)
    print("añadiendo: ",value)
    value = 0.0

print(array_values)

np.append returns an ndarray.

A copy of arr with values appended to axis. Note that append does not occur in-place: a new array is allocated and filled. If axis is None, out is a flattened array.

Check the return section to understand better https://numpy.org/doc/stable/reference/generated/numpy.append.html

ksohan
  • 1,165
  • 2
  • 9
  • 23
  • Sorry, my typing mistake. Can you chack the updated code, please? @ChemiJ – ksohan Aug 20 '21 at 18:39
  • It didnt work too, but i fixed removing it and writing only array_values.append(value) but i cant understand why this works and the other methods didnt. – Cr2 Aug 20 '21 at 18:44
  • Please check the updated code. It should work now as expected @ChemiJ – ksohan Aug 20 '21 at 18:47
  • Use of `np.append` in a loop is in-efficient compared to list append. – hpaulj Aug 20 '21 at 20:43
  • `alist.append(value)` works in place, adding a reference to `value` to the list. `np.append` is really a call to `np.concatenate`, and makes a new array, with copying. `np.append` is not a list append clone, and should not be used in the same way. – hpaulj Aug 20 '21 at 20:47
0

Assuming klines is a 2d numeric dtype array:

In [231]: klines = np.arange(1,13).reshape(4,3)
In [232]: klines
Out[232]: 
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])

we can simple sum across rows with:

In [233]: klines.sum(axis=1)
Out[233]: array([ 6, 15, 24, 33])

the equivalent using your style of iteration:

In [234]: alist = []
     ...: value = 0
     ...: for i in range(4):
     ...:     for j in range(3):
     ...:         value += klines[i,j]
     ...:     alist.append(value)
     ...:     value = 0
     ...: 
In [235]: alist
Out[235]: [6, 15, 24, 33]

Use of np.append is slower and harder to get right.

Even if klines is a list of lists, the sums can be easily done with:

In [236]: [sum(row) for row in klines]
Out[236]: [6, 15, 24, 33]
hpaulj
  • 221,503
  • 14
  • 230
  • 353