How do I get an empty array of any size in python?

后端 未结 8 1825
你的背包
你的背包 2020-12-04 10:57

I basically want a python equivalent of this in C:

int a[x];

but in python I declare an array like:

a = []
<
8条回答
  •  醉话见心
    2020-12-04 11:06

    You can't do exactly what you want in Python (if I read you correctly). You need to put values in for each element of the list (or as you called it, array).

    But, try this:

    a = [0 for x in range(N)]  # N = size of list you want
    a[i] = 5  # as long as i < N, you're okay
    

    For lists of other types, use something besides 0. None is often a good choice as well.

提交回复
热议问题