How to declare an array in python

I want to assign values ​​to different indexes, and not in a sequential way (using append), as would be done in a hash table. How to initialize an array of a given size? What am I doing:

a=[]
for x in range(0,10000):
       a.append(0)

Is there a better way? Also, is there any function like memset () in C ++?

+4
source share
3 answers

As noted in the comments, initialization list(not an array, not a type in Python, although there is a module arrayfor more specialized use) for a group of zeros:

a = [0] * 10000

memset, , 1000 list, :

a[:1000] = [0] * 1000
+4

.

Python , . , 10000 (range(10000)), 10000 :

[0 for _ in range(10000)]

.

, 10000:

[0]*10000

10000 .

+2

memset " CPython:

    from cpython cimport array
    import array

    cdef array.array a = array.array('i', [1, 2, 3])

    # access underlying pointer:
    print a.data.as_ints[0]

    from libc.string cimport memset
    memset(a.data.as_voidptr, 0, len(a) * sizeof(int))
+1

All Articles