Home > Blockchain >  Python - How to delete/hide brackets and "array(value)" when printing an array?
Python - How to delete/hide brackets and "array(value)" when printing an array?

Time:06-05

I'm multiplying a matrix (15,15) with an array (15,1) to just print the values of 6 positions of the new array. However, when I print these new array, python prints the value like these: [[array([20238.49170303])]]. How can I delete all of the brackets and the "array"?

Here is my code:

r = np.dot(kportico, utotal) - f

rp = np.array([[r[0]],
               [r[1]],
               [r[2]],
               [r[12]],
               [r[13]],
               [r[14]]])

rv = np.array([["R1x ="],
               ["R1y ="],
               ["M1 ="],
               ["R5x ="],
               ["R5y ="],
               ["M5 ="]])


uni = np.array([["N"],
                ["N"],
                ["N*m"],
                ["N"],
                ["N"],
                ["N*m"]])

size = len(rv)

print(" ")
print(" ")

if len(rv) == len(rp) and len(rv) == len(uni):
    for x in range(size):
        print(rv[x],rp[x],uni[x])

The print that returns is:

['R1x ='] [[array([20238.49170303])]] ['N']
['R1y ='] [[array([77650.64938379])]] ['N']
['M1 ='] [[array([6.69388101e-09])]] ['N*m']
['R5x ='] [[array([-50238.49170304])]] ['N']
['R5y ='] [[array([86650.64938382])]] ['N']
['M5 ='] [[array([4.07453626e-10])]] ['N*m']

And I would like to make the print much more cleaner:

R1x = 20238.49170303 N
R1y = 77650.64938379 N
M1 = 6.69388101e-09 N*m
R5x = -50238.49170304 N
R5y = 86650.64938382 N
M5 = 4.07453626e-10 N*m

CodePudding user response:

You have created (n,1) shaped arrays

In [283]: x = np.array([[1],[2],[3]]); y = np.array([['one'],['two'],['three']])

In [284]: x
Out[284]: 
array([[1],
       [2],
       [3]])

In [285]: y
Out[285]: 
array([['one'],
       ['two'],
       ['three']], dtype='<U5')

In [286]: x.shape
Out[286]: (3, 1)

If you iterate on the first dimension, the elements are (1,) shaped arrays, and display as such:

In [287]: for i in range(3):
     ...:     print(y[1],x[i])
     ...:     
['two'] [1]
['two'] [2]
['two'] [3]

If the arrays are 1d, flattened (if needed):

In [288]: x1 = x.ravel(); y1 = y.ravel()

In [289]: x1.shape
Out[289]: (3,) 
In [291]: x1,y1
Out[291]: (array([1, 2, 3]), array(['one', 'two', 'three'], dtype='<U5'))

Now y1[0] is a string, and x1[0] is a number:

In [292]: for i in range(3):
     ...:     print(y1[1],x1[i])
     ...:     
two 1
two 2
two 3

And adding a bit of formatting to the print:

In [294]: for i in range(3):
     ...:     print(f'{y1[1]} = {x1[i]}')
     ...:     
two = 1
two = 2
two = 3

You could also use 2d indexing on the 2d arrays:

In [297]: for i in range(3):
     ...:     print(f'{y[1,0]} = {x[i,0]}')
     ...:     
two = 1
two = 2
two = 3

CodePudding user response:

You can create a set of characters which have to be removed from the string and then use the .join() function in this way:

    stringa = ''.join([c for c in stringa if c not in chars])

where:

  • stringa = "{}{}{}".format(rv[x], rp[x], uni[x])
  • chars = ['[',']',"'"]

Here the .join() function just takes the characters inside stringa that are not present in the set of what we could call "the forbidden characters", or better, the characters we want to remove.

As for testing it i wrote down the following code starting from what you provided:

# characters to remove
chars = ['[',']',"'"]

if len(rv) == len(rp) and len(rv) == len(uni):
    for x in range(size):
        stringa = "{}{}{}".format(rv[x], rp[x], uni[x])
        stringa = ''.join([c for c in stringa if c not in chars])
        print(stringa)
  • Related