2

The following code creates two arrays, A and B. A is actually a numpy array of arrays, only because its subarrays don't have the same length.

arr1 = np.arange(2,7)
arr2 = np.arange(5,7)
arr3 = np.arange(7,9)
A = np.array([arr1, arr2, arr3])


arr1 = np.arange(2,4)
arr2 = np.arange(5,7)
arr3 = np.arange(7,9)
B = np.array([arr1, arr2, arr3])

Now, my question is how to force B also to be an array of arrays even if its subarrays have the same length?

Shaki
  • 349
  • 3
  • 8
  • 2
    The surest way of making an object dtype array, is to initialize it as such, eg. `np.empty(3, dtype=object)` (which will be filled with `None`), and assign the individual arrays to it. `arr[:] = [arr1, arr2, arr3]` might work (if broadcasting doesn't get in the way). – hpaulj Jan 01 '20 at 21:47
  • I think you have your `A` and `B` switched. `B` is the standard 2D array, and `A` is an array or arrays in your example – James Jan 01 '20 at 22:05
  • Thanks, James! Corrected. – Shaki Jan 01 '20 at 23:09
  • @hpaulj. How could broadcasting screw things up? (It doesn't in this case at least) – Mad Physicist Jan 02 '20 at 18:29
  • @MadPhysicist, I agree that in this case it doesn't May be it's more of a vague memory of problems in the past. `B[:]=np.ones((3,2))` has problems. `B[:]=list(np.ones((3,2)))` does not. – hpaulj Jan 02 '20 at 18:52

0 Answers0