The dimensions of the output list in python can be implemented using numpy:
import numpy as np a = [[1,2],[3,4]] print(np.array(a).shape)
Extension:
reshape&resize&shape to change the array dimension
reshape function: does not change the original array dimension, has a return value
resize function: directly changes the original array dimension, no return value
shape attribute: directly changes the original array dimension
>>> 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]]]) >>>
More Python For related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to output the dimensions of a list in python. For more information, please follow other related articles on the PHP Chinese website!