python - Print two arrays side by side using numpy -
i'm trying create table of cosines using numpy in python. want have angle next cosine of angle, looks this:
0.0 1.000 5.0 0.996 10.0 0.985 15.0 0.966 20.0 0.940 25.0 0.906 , on.
i'm trying using loop i'm not sure how work. currently, have .
any suggestions?
let's have:
>>> d = np.linspace(0, 360, 10, endpoint=false) >>> c = np.cos(np.radians(d))
if don't mind having brackets , such on side, can concatenate column-wise using np.c_
, , display:
>>> print(np.c_[d, c]) [[ 0.00000000e+00 1.00000000e+00] [ 3.60000000e+01 8.09016994e-01] [ 7.20000000e+01 3.09016994e-01] [ 1.08000000e+02 -3.09016994e-01] [ 1.44000000e+02 -8.09016994e-01] [ 1.80000000e+02 -1.00000000e+00] [ 2.16000000e+02 -8.09016994e-01] [ 2.52000000e+02 -3.09016994e-01] [ 2.88000000e+02 3.09016994e-01] [ 3.24000000e+02 8.09016994e-01]]
but if care removing them, 1 possibility use simple regex:
>>> import re >>> print(re.sub(r' *\n *', '\n', np.array_str(np.c_[d, c]).replace('[', '').replace(']', '').strip())) 0.00000000e+00 1.00000000e+00 3.60000000e+01 8.09016994e-01 7.20000000e+01 3.09016994e-01 1.08000000e+02 -3.09016994e-01 1.44000000e+02 -8.09016994e-01 1.80000000e+02 -1.00000000e+00 2.16000000e+02 -8.09016994e-01 2.52000000e+02 -3.09016994e-01 2.88000000e+02 3.09016994e-01 3.24000000e+02 8.09016994e-01
i'm removing brackets, , passing regex remove spaces on either side in each line.
np.array_str
lets set precision. more control, can use np.array2string
instead.
Comments
Post a Comment