0

I have a numpy array full of 0's such as this

   [0,0,0,0,0,0,0,0,0,0,0,0]

And a list of positions such as this

   [2-4,6-10] 

So what I want to do is iterate through the list of positions and then change the 0's in the numpy array to 1's within the according positions so that I should have a numpy array such as.

  [0,1,1,1,0,1,1,1,1,1,0,0,0]

Hope this is clear enough, if not just let me know.

Thanks.

6
  • Your "list of positions" is effectively [-2, 6, 10] (because 2-4 is -2). Is that what you intended? Commented Apr 26, 2017 at 19:05
  • Sorry my bad, I meant 2-4 and then 6-10 so the positions would be the second to the forth and then the sixth to the tenth. Thanks Commented Apr 26, 2017 at 19:06
  • So how do you represent the 2-4 in python? (the way you represent it will change the answer) Commented Apr 26, 2017 at 19:07
  • That still doesn't make sense. If you type [2-4,6-10] into the Python interpreter and evaluate it you will get [-2, -4]. What data structure do you actually have? Do you have a list of strings? Commented Apr 26, 2017 at 19:07
  • Your output array seems a little inconsistent. Are you sure it's correct? 2-4 covers positions 2 through 4 and 6-10 only covers positions 7 through 10 Commented Apr 26, 2017 at 19:07

2 Answers 2

5

Here's one approach by generating those indices as a concatenated array with np.r_ and then indexing and assigning 1s -

In [64]: a = np.array([0,0,0,0,0,0,0,0,0,0,0,0])

In [65]: pos = np.r_[1:4,5:10]

In [66]: a[pos] = 1

In [67]: a
Out[67]: array([0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0])
Sign up to request clarification or add additional context in comments.

3 Comments

this is a trivial fix; OP needs till index 10. so, it should be np.r_[1:4, 5:10]. As a side note, we could also use range()
@kmario23 OP has 12 elems in input, while 13 in output. So, we need some clarification on it from OP. Posted comment on the same. Changing the ranges should be trivial anyway, so I would leave it to OP to make the needed edits.
Yeah, I noticed the discrepancy. But from his index positions [2-4, 6-10] it is somehow clear that he needs 5 elements (6-10) regardless of the input/output size. Seems like he's following matlab based convention for the index
1

You could use a list of pairs to hold the positions;

l = [(2, 4), (6, 10)]
nl = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

Secondly, use a loop;

for a in l:
  for c, n in enumerate(nl) 
    if (a[0] >= c || a[1] <= c):
       nl[c] = 1

This is by far not the fastest way to do this but it is simple and readable.

As suggested by this user you could use this instead, which is a lot better in my opinion;

nl[a[0]:[a[1]]=[1]*(a[1]-a[0])

3 Comments

This slice assignment might be faster: nl[a[0]:[a[1]]=[1]*(a[1]-a[0])
Sorry - I fixed it now
Glad I could help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.