简体   繁体   中英

Python: Extracting float values from array of lists

during data processing I create an array looking like this:

[array([ 0.08606408]) array([ 0.26071976])
 array([ 0.181566  ,  0.94154611]) array([ 0.1734347 ,  0.94160601])
 array([ 0.17859844,  0.94167483]) array([ 0.16880761,  0.94156277])
 array([ 0.17624151,  0.94149038]) array([ 0.18770433,  0.94181287])
 array([ 0.16707977,  0.94227733]) array([ 0.94162233])
 array([ 0.9426902,  0.9615621]) array([ 0.94195127,  0.96174422])
 array([ 0.94237795,  0.96195226,  0.98059446])
 array([ 0.94249657,  0.96219391,  0.98095329])
 array([ 0.94280697,  0.96286183,  0.98109352])
 array([ 0.94267473,  0.96304417,  0.98252799])]

created in the following manner:

peakpositions = []
peakpositions.append(stuff)

How do I extract the float values into a single 1D numpy array? What I want is this:

[0.08606408, 0.26071976, 0.181566 ... 0.98252799]

Thanks in advance!

You can concatenate your inner arrays :

peakpositions=np.concatenate(peakpositions)

Demo :

>>> l= [[array([ 0.08606408]), array([ 0.26071976]),
       array([ 0.181566  ,  0.94154611]),
       array([ 0.1734347 ,  0.94160601]),
       array([ 0.17859844,  0.94167483]),
       array([ 0.16880761,  0.94156277]),
       array([ 0.17624151,  0.94149038]),
       array([ 0.18770433,  0.94181287]),
       array([ 0.16707977,  0.94227733]), array([ 0.94162233]),
       array([ 0.9426902,  0.9615621]), array([ 0.94195127,  0.96174422]),
       array([ 0.94237795,  0.96195226,  0.98059446]),
       array([ 0.94249657,  0.96219391,  0.98095329]),
       array([ 0.94280697,  0.96286183,  0.98109352]),
       array([ 0.94267473,  0.96304417,  0.98252799])]
>>> np.concatenate(l)
array([ 0.08606408,  0.26071976,  0.181566  ,  0.94154611,  0.1734347 ,
        0.94160601,  0.17859844,  0.94167483,  0.16880761,  0.94156277,
        0.17624151,  0.94149038,  0.18770433,  0.94181287,  0.16707977,
        0.94227733,  0.94162233,  0.9426902 ,  0.9615621 ,  0.94195127,
        0.96174422,  0.94237795,  0.96195226,  0.98059446,  0.94249657,
        0.96219391,  0.98095329,  0.94280697,  0.96286183,  0.98109352,
        0.94267473,  0.96304417,  0.98252799])
>>> 

Create an empty list/array and extend it.

array_1d = []
for array in arrays:
    array_1d.extend(array)

That should do the magic.

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