Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6347487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:13:30+00:00 2026-05-24T21:13:30+00:00

Simple question here: I’m trying to get an array that alternates values (1, -1,

  • 0

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). Thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:13:31+00:00Added an answer on May 24, 2026 at 9:13 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question here: is there any way to convert from a jagged array to
Here's a very simple question. I have an SP that inserts a row into
Simple question, I just want to select the text from the <Template> tag. Here's
A little stuck here. I have a simple question I guess. Given the following
Simple question here that I can't seem to find an answer for: Once a
Here's a simple question. I have a jqGrid that's working great but I want
Simple question, but I'll bet that asking on here will probably be more straight
Really simple question here: For a PHP-driven RSS feed, am I just overwriting the
Very simple question here. I have a double that I wish to convert back
I got a simple question here Let's suppose we have a codebase that employs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.