How to create an integer array in Python?

前端 未结 8 707
旧巷少年郎
旧巷少年郎 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:44

    Use the array module. With it you can store collections of the same type efficiently.

    >>> import array
    >>> import itertools
    >>> a = array_of_signed_ints = array.array("i", itertools.repeat(0, 10))
    

    For more information - e.g. different types, look at the documentation of the array module. For up to 1 million entries this should feel pretty snappy. For 10 million entries my local machine thinks for 1.5 seconds.

    The second parameter to array.array is a generator, which constructs the defined sequence as it is read. This way, the array module can consume the zeros one-by-one, but the generator only uses constant memory. This generator does not get bigger (memory-wise) if the sequence gets longer. The array will grow of course, but that should be obvious.

    You use it just like a list:

    >>> a.append(1)
    >>> a.extend([1, 2, 3])
    >>> a[-4:]
    array('i', [1, 1, 2, 3])
    >>> len(a)
    14
    

    ...or simply convert it to a list:

    >>> l = list(a)
    >>> len(l)
    14
    

    Surprisingly

    >>> a = [0] * 10000000
    

    is faster at construction than the array method. Go figure! :)

    0 讨论(0)
  • 2020-12-24 01:46
    a = 10 * [0]
    

    gives you an array of length 10, filled with zeroes.

    0 讨论(0)
  • 2020-12-24 01:50
    import numpy as np
    
    new_array=np.linspace(0,10,11).astype('int')
    

    An alternative for casting the type when the array is made.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-24 02:02
    import random
    
    def random_zeroes(max_size):
      "Create a list of zeros for a random size (up to max_size)."
      a = []
      for i in xrange(random.randrange(max_size)):
        a += [0]
    

    Use range instead if you are using Python 3.x.

    0 讨论(0)
  • 2020-12-24 02:03

    If you need to initialize an array fast, you might do it by blocks instead of with a generator initializer, and it's going to be much faster. Creating a list by [0]*count is just as fast, still.

    import array
    
    def zerofill(arr, count):
        count *= arr.itemsize
        blocksize = 1024
        blocks, rest = divmod(count, blocksize)
        for _ in xrange(blocks):
            arr.fromstring("\x00"*blocksize)
        arr.fromstring("\x00"*rest)
    
    def test_zerofill(count):
        iarr = array.array('i')
        zerofill(iarr, count)
        assert len(iarr) == count
    
    def test_generator(count):
        iarr = array.array('i', (0 for _ in xrange(count)))
        assert len(iarr) == count
    
    def test_list(count):
        L = [0]*count
        assert len(L) == count
    
    if __name__ == '__main__':
        import timeit
        c = 100000
        n = 10
        print timeit.Timer("test(c)", "from __main__ import c, test_zerofill as test").repeat(number=n)
        print timeit.Timer("test(c)", "from __main__ import c, test_generator as test").repeat(number=n)
        print timeit.Timer("test(c)", "from __main__ import c, test_list as test").repeat(number=n)
    

    Results:

    (array in blocks) [0.022809982299804688, 0.014942169189453125, 0.014089107513427734]
    (array with generator) [1.1884641647338867, 1.1728270053863525, 1.1622772216796875]
    (list) [0.023866891860961914, 0.035660028457641602, 0.023386955261230469]
    
    0 讨论(0)
提交回复
热议问题