Home > Software design >  Remove array() when printing
Remove array() when printing

Time:05-23

In my code below I want to print the lists x_points and y_points, but when I try printing y_points I get something like

[array(6.), array(6.3248), array(6.6144), array(6.8716), array(7.0992), array(7.3), array(7.4768), array(7.6324), array(7.7696), array(7.8912), array(8.), array(8.0988), array(8.1904), array(8.2776), array(8.3632), array(8.45), array(8.5408), array(8.6384), array(8.7456), array(8.8652), array(9.), array(9.1528), array(9.3264), array(9.5236), array(9.7472), array(10.)]

and I want to remove the array() part so I can copy this list into a c program, but I don't know how to remove the array() part

#adds valid x points to the list
i = 0
while i <= valid_x_points:
    x_points.append(x_coords[0]   (i * step))
    i = i   1

#calculate the value at given x points and adds them to a list
points = 0
while points < len(x_points):
    y_points.append(f_interpid(x_points[points]))
    points = points   1

print(x_points)
print(y_points)

CodePudding user response:

The function numpy.ndarray.tolist() converts a Numpy array to a Python list. You have a python list of numpy elements, so the following piece of code should provide you a starting point

from numpy import array

la = [array(6.), array(6.3248), array(6.6144), array(6.8716), array(7.0992), array(7.3), array(7.4768), array(7.6324), array(7.7696), array(7.8912), array(8.), array(8.0988), array(8.1904), array(8.2776), array(8.3632), array(8.45), array(8.5408), array(8.6384), array(8.7456), array(8.8652), array(9.), array(9.1528), array(9.3264), array(9.5236), array(9.7472), array(10.)]

lb = array(la).tolist()
print(list(lb))

CodePudding user response:

Looks like you have an object dtype array that contains 1 element arrays. How did you get that?

To recreate that from your display I have use:

In [341]: array=np.array; res=np.empty(4, object); res[:]=[array(6.), array(6.3248), array(6.6144), array(6.8716)]

the repr display of that array:

In [342]: res
Out[342]: 
array([array(6.), array(6.3248), array(6.6144), array(6.8716)],
      dtype=object)

and its print matches yours:

In [343]: print(res)
[array(6.) array(6.3248) array(6.6144) array(6.8716)]

That "array" isn't gratuitous decoration. It tells us something about the nature of that array.

One way to turn such an array into a numeric one is:

In [344]: np.hstack(res)
Out[344]: array([6.    , 6.3248, 6.6144, 6.8716])

edit

If it really is a list of arrays:

In [347]: alist = [array(6.), array(6.3248), array(6.6144), array(6.8716), array(7.0992)]

In [348]: alist
Out[348]: [array(6.), array(6.3248), array(6.6144), array(6.8716), array(7.0992)]

In [349]: print(alist)
[array(6.), array(6.3248), array(6.6144), array(6.8716), array(7.0992)]

hstack still works:

In [350]: np.hstack(alist)
Out[350]: array([6.    , 6.3248, 6.6144, 6.8716, 7.0992])

but so will np.array:

In [351]: np.array(alist)
Out[351]: array([6.    , 6.3248, 6.6144, 6.8716, 7.0992])

How are you doing the copy to c ? Just a copy-n-paste? Or some sort of buffer protocol?

For copy-n-paste the tolist rendition may be a bit easier to use:

In [352]: np.array(alist).tolist()
Out[352]: [6.0, 6.3248, 6.6144, 6.8716, 7.0992]
  • Related