How to output the dimensions of a list in python

Release: 2019-07-01 10:21:53
Original
9090 people have browsed it

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)
Copy after login

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]]])
>>>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template