How can I get an array of alternating values in python?

后端 未结 7 1877
臣服心动
臣服心动 2020-12-30 01:06

Simple question here:

I\'m trying to get an array that alternates values (1, -1, 1, -1.....) for a given length. np.repeat just gives me (1, 1, 1, 1,-1, -1,-1, -1

相关标签:
7条回答
  • 2020-12-30 01:40

    Use numpy.tile!

    import numpy
    a = numpy.tile([1,-1], 15)
    
    0 讨论(0)
  • 2020-12-30 01:41

    Maybe you're looking for itertools.cycle?

    list_ = (1,-1,2,-2)  # ,3,-3, ...
    
    for n, item in enumerate(itertools.cycle(list_)):
        if n==30:
            break
    
        print item
    
    0 讨论(0)
  • 2020-12-30 01:43

    If you want a memory efficient solution, try this:

    def alternator(n):
        for i in xrange(n):
            if i % 2 == 0:
                yield 1
            else:
                yield -1
    

    Then you can iterate over the answers like so:

    for i in alternator(n):
        # do something with i
    
    0 讨论(0)
  • 2020-12-30 01:46

    I like @Benjamin's solution. An alternative though is:

    import numpy as np
    a = np.empty((15,))
    a[::2] = 1
    a[1::2] = -1
    

    This also allows for odd-length lists.

    EDIT: Also just to note speeds, for a array of 10000 elements

    import numpy as np
    from timeit import Timer
    
    if __name__ == '__main__':
    
        setupstr="""
    import numpy as np
    N = 10000
    """
    
        method1="""
    a = np.empty((N,),int)
    a[::2] = 1
    a[1::2] = -1
    """
    
        method2="""
    a = np.tile([1,-1],N)
    """
    
        method3="""
    a = np.array([1,-1]*N)   
    """
    
        method4="""
    a = np.array(list(itertools.islice(itertools.cycle((1,-1)), N)))    
    """
        nl = 1000
        t1 = Timer(method1, setupstr).timeit(nl)
        t2 = Timer(method2, setupstr).timeit(nl)
        t3 = Timer(method3, setupstr).timeit(nl)
        t4 = Timer(method4, setupstr).timeit(nl)
    
        print 'method1', t1
        print 'method2', t2
        print 'method3', t3
        print 'method4', t4
    

    Results in timings of:

    method1 0.0130500793457
    method2 0.114426136017
    method3 4.30518102646
    method4 2.84446692467
    

    If N = 100, things start to even out but starting with the empty numpy arrays is still significantly faster (nl changed to 10000)

    method1 0.05735206604
    method2 0.323992013931
    method3 0.556654930115
    method4 0.46702003479
    

    Numpy arrays are special awesome objects and should not be treated like python lists.

    0 讨论(0)
  • 2020-12-30 01:46

    use multiplication:

    [1,-1] * n
    
    0 讨论(0)
  • 2020-12-30 01:51

    use resize():

    In [38]: np.resize([1,-1], 10) # 10 is the length of result array
    Out[38]: array([ 1, -1,  1, -1,  1, -1,  1, -1,  1, -1])
    

    it can produce odd-length array:

    In [39]: np.resize([1,-1], 11)
    Out[39]: array([ 1, -1,  1, -1,  1, -1,  1, -1,  1, -1,  1])
    
    0 讨论(0)
提交回复
热议问题