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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
I like @Benjamin’s solution. An alternative though is:
This also allows for odd-length lists.
EDIT: Also just to note speeds, for a array of 10000 elements
Results in timings of:
If
N = 100, things start to even out but starting with the empty numpy arrays is still significantly faster (nlchanged to 10000)Numpy arrays are special awesome objects and should not be treated like python lists.