Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42451 > unrolled thread
| Started by | Ana Dionísio <anadionisio257@gmail.com> |
|---|---|
| First post | 2013-04-01 03:14 -0700 |
| Last post | 2013-04-01 13:28 +0100 |
| Articles | 7 — 5 participants |
Back to article view | Back to comp.lang.python
How to do this? Ana Dionísio <anadionisio257@gmail.com> - 2013-04-01 03:14 -0700
Re: How to do this? Vincent Vande Vyvre <vincent.vandevyvre@swing.be> - 2013-04-01 12:58 +0200
Re: How to do this? Ana Dionísio <anadionisio257@gmail.com> - 2013-04-01 04:08 -0700
Re: How to do this? Peter Otten <__peter__@web.de> - 2013-04-01 13:32 +0200
Re: How to do this? Dave Angel <davea@davea.name> - 2013-04-01 07:53 -0400
Re: How to do this? Ana Dionísio <anadionisio257@gmail.com> - 2013-04-01 04:08 -0700
Re: How to do this? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-04-01 13:28 +0100
| From | Ana Dionísio <anadionisio257@gmail.com> |
|---|---|
| Date | 2013-04-01 03:14 -0700 |
| Subject | How to do this? |
| Message-ID | <83a54022-576d-465b-b6bd-7e21cde59d99@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Vincent Vande Vyvre <vincent.vandevyvre@swing.be> |
|---|---|
| Date | 2013-04-01 12:58 +0200 |
| Message-ID | <mailman.4051.1364813918.2939.python-list@python.org> |
| In reply to | #42451 |
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
--
Vincent V.V.
Oqapy <https://launchpad.net/oqapy> . Qarte
<https://launchpad.net/qarte> . PaQager <https://launchpad.net/paqager>
[toc] | [prev] | [next] | [standalone]
| From | Ana Dionísio <anadionisio257@gmail.com> |
|---|---|
| Date | 2013-04-01 04:08 -0700 |
| Message-ID | <29fa109e-8ed0-4b1b-a9c0-473e2bace687@googlegroups.com> |
| In reply to | #42452 |
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?
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-04-01 13:32 +0200 |
| Message-ID | <mailman.4053.1364815917.2939.python-list@python.org> |
| In reply to | #42453 |
Ana Dionísio wrote:
> 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[i] = .17
...
>>> 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 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. ])
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-04-01 07:53 -0400 |
| Message-ID | <mailman.4054.1364817250.2939.python-list@python.org> |
| In reply to | #42453 |
On 04/01/2013 07:08 AM, Ana Dionísio wrote:
> [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
--
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Ana Dionísio <anadionisio257@gmail.com> |
|---|---|
| Date | 2013-04-01 04:08 -0700 |
| Message-ID | <mailman.4052.1364814526.2939.python-list@python.org> |
| In reply to | #42452 |
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?
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-04-01 13:28 +0100 |
| Message-ID | <mailman.4055.1364819291.2939.python-list@python.org> |
| In reply to | #42451 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web