How to do this?

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Apr 1 08:28:51 EDT 2013


On 01/04/2013 11:14, Ana Dionísio wrote:
> So I have this script:
>
> "
> from numpy import array

Are you aware that this overrides the Python builtin array?  It's 
usually but not always better to do this

import numpy as np
vt = np.array(vt, dtype=dict)

>
> vt=[0]*20
> vt = array(vt, dtype=dict)
>
> for t in range(20):
>      if t == 4:
>          vt[t]=1
>
>      else:
>          vt[t]=0

Why the loop, you've already initialise the array to zeroes?

> "
>
> And have this output:
>
> [0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
>
> What I need is when t == 4 I need to put 1 in that position and in the next 3, for example the following output:
>
> [0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0]
>
> Do you have any suggestions?

Use slicing.
vt[4:8] = 1

>
>
> I'm sorry if I can't explain this in a better way, English is not my first language
>
> Thank you
>

-- 
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence




More information about the Python-list mailing list