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..
l = [1,2,3,4]
l = range(10)
Can I initialize a list by a given size,like c++,and do not do any assignment?
解决方案
(tl;dr: The exact answer to your question is numpy.empty or numpy.empty_like, but you likely don't care and can get away with using myList = [None]*10000.)
Simple methods
You can initialize your list to all the same element. Whether it semantically makes sense to use a non-numeric value (that will give an error later if you use it, which is a good thing) or something like 0 (unusual? maybe useful if you're writing a sparse matrix or the 'default' value should be 0 and you're not worried about bugs) is up to you:
>>