How to create a fix size list in python?

后端 未结 9 1780
不思量自难忘°
不思量自难忘° 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:40

    Python has nothing built-in to support this. Do you really need to optimize it so much as I don't think that appending will add that much overhead.

    However, you can do something like l = [None] * 1000.

    Alternatively, you could use a generator.

    0 讨论(0)
  • 2020-12-04 21:42
    fix_array = numpy.empty(n, dtype = object)
    

    where n is the size of your array

    though it works, it may not be the best idea as you have to import a library for this purpose. Hope this helps!

    0 讨论(0)
  • 2020-12-04 21:43
    your_list = [None]*size_required
    
    0 讨论(0)
提交回复
热议问题