简体   繁体   中英

appending lists of list with column values from an array

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!

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]])

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM