Assigning the same value to multiple positions in a slice

This is the place for queries that don't fit in any of the other categories.

Assigning the same value to multiple positions in a slice

Postby sapphyr_raven » Wed Apr 02, 2014 8:05 am

Assigning the same value to multiple positions in a slice

Basically,
Code: Select all
mask[pos-2:pos+2] = True
is what I would like to achieve but it doesn’t seem to work...

Thank you in advance for your help!
sapphyr_raven
 
Posts: 2
Joined: Wed Apr 02, 2014 8:04 am

Re: Assigning the same value to multiple positions in a slic

Postby stranac » Wed Apr 02, 2014 8:31 am

If you want to assign to a slice, you'll need a sequence of equal size:
Code: Select all
>>> mask = [False] * 10
>>> mask[2:6] = [True] * 3
>>> mask
[False, False, True, True, True, False, False, False, False]

Or, if you use something like numpy arrays, what you tried would work as well.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1149
Joined: Thu Feb 07, 2013 3:42 pm

Re: Assigning the same value to multiple positions in a slic

Postby sapphyr_raven » Wed Apr 02, 2014 9:51 am

Thank you! That makes so much more sense.
sapphyr_raven
 
Posts: 2
Joined: Wed Apr 02, 2014 8:04 am


Return to General Coding Help

Who is online

Users browsing this forum: stranac and 3 guests