Home >Backend Development >Python Tutorial >How to output the dimensions of a list in python

How to output the dimensions of a list in python

尚
Original
2019-07-01 10:21:539142browse

How to output the dimensions of a list in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn