[Numpy-discussion] three-dim array

Eric Wieser wieser.eric+numpy at gmail.com
Wed Dec 26 01:58:41 EST 2018


In the latest version of numpy, this runs without an error, although may or
may not be what you want:

In [1]: np.array([[1,2],[[1,2],[3,4]]])
Out[1]:
array([[1, 2],
       [list([1, 2]), list([3, 4])]], dtype=object)

Here the result is a 2x2 array, where some elements are numbers and others
are lists.
​

On Wed, 26 Dec 2018 at 06:23 Mark Alexander Mikofski <mikofski at berkeley.edu>
wrote:

> I believe numpy arrays must be rectangular, yours is jagged, instead try
>
> >>> x3d = np.array([[[1, 2], [1, 2], [3, 4]]])
> >>> x3d.shape
> (1, 3, 2)
>
> Note: 3 opening brackets, yours has 2
> And single brackets around the 3 innermost arrays, yours has single
> brackets for the 1st, and double brackets around the 2nd and 3rd
>
>
> On Tue, Dec 25, 2018, 6:20 PM Jeff <jeffp at vodafonemail.de wrote:
>
>> hello,
>>
>> sorry newbe to numpy.
>>
>> I want to define a three-dim array.
>> I know this works:
>>
>>  >>> np.array([[[1,2],[3,4]],[[5,6],[7,8]]])
>> array([[[1, 2],
>>          [3, 4]],
>>
>>         [[5, 6],
>>          [7, 8]]])
>>
>> But can you tell why this doesnt work?
>>
>>  >>> np.array([[1,2],[[1,2],[3,4]]])
>> Traceback (most recent call last):
>>    File "<stdin>", line 1, in <module>
>> ValueError: setting an array element with a sequence.
>>
>>
>> Thank you.
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at python.org
>> https://mail.python.org/mailman/listinfo/numpy-discussion
>>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20181226/be797638/attachment.html>


More information about the NumPy-Discussion mailing list