Home > Blockchain >  appending lists of list with column values from an array
appending lists of list with column values from an array

Time:12-17

I have 3 arrays of 3x1 in a list, that I want to append to a list of lists, where each list is a single row 1x3 from each array.

for example, convert this:

[array([['0.6913'],
       ['0.7279'],
       ['0.724']], dtype=object), 
array([['0.6943'],
       ['0.7206'],
       ['0.714']], dtype=object), 
array([['0.6456'],
       ['0.7447'],
       ['0.7378']],dtype=object)]

to:

   [[0.6913, 0.6943, 0.6456],
    [0.7279, 0.7206, 0.7447],
    [0.724, 0.714, 0.7378]]

How can I do this? thank you in advance!

CodePudding user response:

Use numpy.hstack:

import numpy as np

a = [
    np.array([['0.6913'], ['0.7279'], ['0.724']], dtype=object),
    np.array([['0.6943'], ['0.7206'], ['0.714']], dtype=object),
    np.array([['0.6456'], ['0.7447'], ['0.7378']], dtype=object),
]

np.hstack(a)
array([['0.6913', '0.6943', '0.6456'],
       ['0.7279', '0.7206', '0.7447'],
       ['0.724', '0.714', '0.7378']], dtype=object)

To convert to float use .astype

np.hstack(a).astype(float)
array([[0.6913, 0.6943, 0.6456],
       [0.7279, 0.7206, 0.7447],
       [0.724 , 0.714 , 0.7378]])

CodePudding user response:

Try the following code:- Suppose the name of the list you have in the beginning be K.

ans=[]
for i in range(3):
    l=[]
    for j in K[i]:
        l.append(j)
    ans.append(l)
  • Related