How to create a fix size list in python?

后端 未结 9 1839
不思量自难忘°
不思量自难忘° 2020-12-04 20:53

In C++, I can create a array like...

int* a = new int[10];

in python,I just know that I can declare a list,than append some items,or like..

9条回答
  •  心在旅途
    2020-12-04 21:37

    Note also that when you used arrays in C++ you might have had somewhat different needs, which are solved in different ways in Python:

    1. You might have needed just a collection of items; Python lists deal with this usecase just perfectly.
    2. You might have needed a proper array of homogenous items. Python lists are not a good way to store arrays.

    Python solves the need in arrays by NumPy, which, among other neat things, has a way to create an array of known size:

    from numpy import *
    
    l = zeros(10)
    

提交回复
热议问题