简体   繁体   中英

Python/Numpy joining subarrays of an array together

Currently I have a list of arrays as follows:

array =
[array([100000.        , 100848.06652142,  99648.67144694, 102047.67944271,
       103655.99048427, 104602.87678005, 104597.83419837, 103505.42736768,
       104976.01311214, 104457.34603404, 105855.46549162, 105613.30235519,
       105212.71840922, 107647.5054673 ])
 array([107647.5054673 , 106891.82007643, 106979.91405552, 106030.74186486,
       107856.04281712, 108502.71948581, 106557.2401056 , 105659.59829843,
       105663.01875745, 107300.69453018, 106336.34733019, 107584.99034227,
       108089.2320392 , 106786.91702337])
 array([106786.91702337, 104416.74000465, 101289.12525402, 101932.58219813,
       102625.04352578, 101767.46209616, 103345.4263012 , 102816.73322055,
       102417.59316407, 104439.37518975, 103755.22627215, 103817.9744686 ,
       107872.40234863, 108110.9662065 ])
 array([108110.9662065 , 109544.86827069, 111072.22392645, 112618.46235766,
       113847.1768898 , 116708.86391903, 115790.02599715, 115614.72242411,
       119225.88436354, 121991.38468934, 123304.85972848, 125571.38040251,
       122456.3667249 ])
 array([122456.3667249 , 127497.74699282, 128659.85495604, 125813.77115906,
       129008.46450085, 128111.00914756, 123039.92607546, 124723.87932757,
       124181.57385836, 125134.9276196 , 126027.8631434 , 129304.85119148,
       128912.58600657])]

What would be the best possible way to join all these subarrays back together into one large array? I tried np.concatenate() but that did not work.

SOLUTION

For whatever reason, my output would not put commas between the subarrays, once I was able to find the fix for that np.concatenate() did indeed work.

concatenate works.. I think you forgot some commas in your example data between each array in the list.

import numpy as np
a = [np.array([1,2,3]), np.array(['4','5','6'])]

np.concatenate(a)

outputs

array(['1', '2', '3', '4', '5', '6']
np.concatenate(array)

The only problem, in your example, array in the list array (now, do you see how bad it is to use confusing names) should become np.array and you should add commas after each numpy.ndarray in the list array :

array = [np.array([100000., 100848.06652142,  99648.67144694, 102047.67944271,
       103655.99048427, 104602.87678005, 104597.83419837, 103505.42736768,
       104976.01311214, 104457.34603404, 105855.46549162, 105613.30235519,
       105212.71840922, 107647.5054673 ]),
 np.array([107647.5054673 , 106891.82007643, 106979.91405552, 106030.74186486,
       107856.04281712, 108502.71948581, 106557.2401056 , 105659.59829843,
       105663.01875745, 107300.69453018, 106336.34733019, 107584.99034227,
       108089.2320392 , 106786.91702337]),
 np.array([106786.91702337, 104416.74000465, 101289.12525402, 101932.58219813,
       102625.04352578, 101767.46209616, 103345.4263012 , 102816.73322055,
       102417.59316407, 104439.37518975, 103755.22627215, 103817.9744686 ,
       107872.40234863, 108110.9662065 ]),
 np.array([108110.9662065 , 109544.86827069, 111072.22392645, 112618.46235766,
       113847.1768898 , 116708.86391903, 115790.02599715, 115614.72242411,
       119225.88436354, 121991.38468934, 123304.85972848, 125571.38040251,
       122456.3667249 ]),
 np.array([122456.3667249 , 127497.74699282, 128659.85495604, 125813.77115906,
       129008.46450085, 128111.00914756, 123039.92607546, 124723.87932757,
       124181.57385836, 125134.9276196 , 126027.8631434 , 129304.85119148,
       128912.58600657])]

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