PHP前端开发

如何输出python中list的维度

百变鹏仔 1个月前 (01-23) #Python
文章标签 维度

python中输出list的维度可以使用numpy来实现:

import numpy as npa = [[1,2],[3,4]]print(np.array(a).shape)

扩展:

reshape&resize&shape改变数组维度

reshape函数:不改变原数组维度,有返回值
resize函数:直接改变原数组维度,无返回值
shape属性:直接改变原数组维度

>>> import numpy as np>>> a=np.arange(12)>>> aarray([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])>>> a.reshape(2,6) array([[ 0,  1,  2,  3,  4,  5],       [ 6,  7,  8,  9, 10, 11]])>>> aarray([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])>>> a.reshape(2,3,2)  array([[[ 0,  1],          [ 2,  3],        [ 4,  5]],       [[ 6,  7],        [ 8,  9],        [10, 11]]])>>> a.resize(2,6)>>> a>>> array([[ 0,  1,  2,  3,  4,  5],       [ 6,  7,  8,  9, 10, 11]])>>> a.shape=(2,6)>>> aarray([[ 0,  1,  2,  3,  4,  5],       [ 6,  7,  8,  9, 10, 11]])>>> a.shape=(2,3,2)>>> aarray([[[ 0,  1],        [ 2,  3],        [ 4,  5]],       [[ 6,  7],        [ 8,  9],        [10, 11]]])>>>

更多Python相关技术文章,请访问Python教程栏目进行学习!

立即学习“Python免费学习笔记(深入)”;