How to create an integer array in Python?

前端 未结 8 725
旧巷少年郎
旧巷少年郎 2020-12-24 01:11

It should not be so hard. I mean in C,

int a[10]; 

is all you need. How to create an array of all zeros for a random size. I know the zero

8条回答
  •  臣服心动
    2020-12-24 01:53

    two ways:

    x = [0] * 10
    x = [0 for i in xrange(10)]
    

    Edit: replaced range by xrange to avoid creating another list.

    Also: as many others have noted including Pi and Ben James, this creates a list, not a Python array. While a list is in many cases sufficient and easy enough, for performance critical uses (e.g. when duplicated in thousands of objects) you could look into python arrays. Look up the array module, as explained in the other answers in this thread.

提交回复
热议问题