I'd like to split my array into multiple ones: The original array looks as follows:
array([[228.6311346 , 228.6311346 , 228.6311346 ],
[418.57914851, 0. , 228.321311 ],
[416.83133465, 0. , 723.25171282]])
The resulting arrays should look like this:
array1([228.6311346, 418.57914851, 416.83133465])
array2([228.6311346, 0., 0.])
array3([228.6311346, 228.321311, 723.25171282])
mkrieger1
24.2k7 gold badges68 silver badges84 bronze badges
1 Answer 1
for i in range(len(array)):
exec("array"+str(i+1)+"=array["+str(i)+"]")
It will create your variable dynamically and assign the corresponding value to it.
array = [[1],[2],[3]]
print(array0) // outputs [1] after running above code
You can also use numpy.hsplit Doc Here
numpy.hsplit(ary, indices_or_sections)
# Split an array into multiple sub-arrays of equal size.
answered Jun 8, 2021 at 19:28
Saket Anand
351 silver badge11 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
0x5453
split/hsplit is the correct answer. exec is a bad idea (despite the fact that it more closely matches OP's desired output).lang-py
np.hsplit(x,3)array1 = array[0,:]andarray2 = array[1,:], etc.