0

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
asked Jun 8, 2021 at 19:14
8
  • Hmm would you like them to be printed or to be assigned to a variable? Commented Jun 8, 2021 at 19:16
  • numpy.org/doc/stable/reference/generated/numpy.hsplit.html np.hsplit(x,3) Commented Jun 8, 2021 at 19:17
  • These are numpy arrays? You can use array1 = array[0,:] and array2 = array[1,:], etc. Commented Jun 8, 2021 at 19:17
  • Does this answer your question? How to access the ith column of a NumPy multidimensional array? Commented Jun 8, 2021 at 19:17
  • 1
    @Chris exactly what I was looking for, thank you! Commented Jun 8, 2021 at 19:20

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

split/hsplit is the correct answer. exec is a bad idea (despite the fact that it more closely matches OP's desired output).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.