How to do this?

A

Ana Dionísio

So I have this script:

"
from numpy import array

vt=[0]*20
vt = array(vt, dtype=dict)

for t in range(20):
if t == 4:
vt[t]=1

else:
vt[t]=0
"

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?


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

Thank you
 
V

Vincent Vande Vyvre

Le 01/04/13 12:14, Ana Dionísio a écrit :
for t in range(20):
if t == 4:
vt[t]=1
Sets only the needed values:

for t in range(4, 8):
vt[t]=1
 
A

Ana Dionísio

Nice! Thank you!

And if I need something like this?

[0 0 0 0 0.17 0.17 0.17 0.17 0 0 0 0.17 0.17 0.17 0 0 0 0 0 0 0]

How can I do this?
 
A

Ana Dionísio

Nice! Thank you!

And if I need something like this?

[0 0 0 0 0.17 0.17 0.17 0.17 0 0 0 0.17 0.17 0.17 0 0 0 0 0 0 0]

How can I do this?
 
P

Peter Otten

Ana said:
Nice! Thank you!

And if I need something like this?

[0 0 0 0 0.17 0.17 0.17 0.17 0 0 0 0.17 0.17 0.17 0 0 0 0 0 0 0]

How can I do this?

With vanilla Python:
vt = [0] * 20
for i, v in enumerate(vt):
.... if 4 <= i < 8 or 13 <= i < 16:
.... vt = .17
....[0, 0, 0, 0, 0.17, 0.17, 0.17, 0.17, 0, 0, 0, 0, 0, 0.17, 0.17, 0.17, 0, 0,
0, 0]

With vanilla Python using slices:
vt = [0] * 20
vt[4:8] = [.17]*4
vt[13:16] = [.17]*3
vt
[0, 0, 0, 0, 0.17, 0.17, 0.17, 0.17, 0, 0, 0, 0, 0, 0.17, 0.17, 0.17, 0, 0,
0, 0]

With numpy:
vt = numpy.zeros(20)
vt[4:8] = vt[13:16] = .17
vt
array([ 0. , 0. , 0. , 0. , 0.17, 0.17, 0.17, 0.17, 0. ,
0. , 0. , 0. , 0. , 0.17, 0.17, 0.17, 0. , 0. ,
0. , 0. ])
 
D

Dave Angel

[0 0 0 0 0.17 0.17 0.17 0.17 0 0 0 0.17 0.17 0.17 0 0 0 0 0 0 0]


I'd do
res = "[0 0 0 0 0.17 0.17 0.17 0.17 0 0 0 0.17 0.17 0.17 0 0 0 0 0 0 0]"

Unless there's a pattern you're trying to accomplish (like maybe next
time you want to do "it" for a list of two million items), there's
little point in writing code to do what you've already predetermined.
And if there is a pattern, you'd probably better let us know.

But if it's for fun,



vt = [0] * 21
for index, val in enumerate(vt):
if 3<index<8 or 10<index<14:
vt[index] = 0.17


or


vt = [0] * 21
for index, val in enumerate(vt):
if index in (4,5,6,7,11,12,13):
vt[index] = 0.17
 
M

Mark Lawrence

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,814
Members
47,359
Latest member
Claim Bitcoin Earnings. $

Latest Threads

Top