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的維度的詳細內容。更多資訊請關注PHP中文網其他相關文章!