对于一个list,可以通过numpy.array()方法转为array,根据list的不同情况,得到的array也有所不同:
当list的维度一样的时候,可以解释为通常的array,即:
>>> import numpy as np
>>>
>>> a = [[1,2,3],[4,5,6]]
>>> a_array = np.array(a)
>>> print(a_array)
[[1 2 3]
[4 5 6]]
>>>
但是,当list的维度不一样的时候,则会被解释为多个object,即:
>>> import numpy as np
>>>
>>> a = [[1,2,3],[4,5,6,7]]
>>> a_array = np.array(a)
<stdin>:1: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
>>> print(a_array.shape)
(2,)
>>>
要想访问此时的a_array的对象,注意到,它其实只有一个维度(2,)因此:
>>> a_array[0,:] # erro
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
>>> a_array[0,] # correct
[1, 2, 3]