时间:2021-07-01 10:21:17 帮助过:2738人阅读
python中输出list的维度可以使用numpy来实现:
import numpy as np a = [[1,2],[3,4]] print(np.array(a).shape)
扩展:
reshape&resize&shape改变数组维度
reshape函数:不改变原数组维度,有返回值
resize函数:直接改变原数组维度,无返回值
shape属性:直接改变原数组维度
>>> import numpy as np >>> a=np.arange(12) >>> a array([ 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]]) >>> a array([ 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) >>> a array([[ 0, 1, 2, 3, 4, 5], [ 6, 7, 8, 9, 10, 11]]) >>> a.shape=(2,3,2) >>> a array([[[ 0, 1], [ 2, 3], [ 4, 5]], [[ 6, 7], [ 8, 9], [10, 11]]]) >>>
更多Python相关技术文章,请访问Python教程栏目进行学习!
以上就是如何输出python中list的维度的详细内容,更多请关注Gxl网其它相关文章!