pythonnumpy

Why can't I create a numpy array like this: array([1, 2], 3)


from numpy import array
test_list = [[1,2],3]
x = array(test_list) #ValueError: setting an array element with a sequence.

Basically, I have a point with 2 coordinates and a scale and I was trying to put several on a ndarray but I can't do it right now. I could just use [1,2,3] but I'm curious about why I can't store that type of list in an array.


Solution

  • It's failing because the array is non-rectangular. If we change the 3 to [3, 4] then it works.

    >>> array([[1, 2], [3, 4]])
    array([[1, 2],
           [3, 4]])