Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42455

Re: How to do this?

From Peter Otten <__peter__@web.de>
Subject Re: How to do this?
Date 2013-04-01 13:32 +0200
Organization None
References <83a54022-576d-465b-b6bd-7e21cde59d99@googlegroups.com> <mailman.4051.1364813918.2939.python-list@python.org> <29fa109e-8ed0-4b1b-a9c0-473e2bace687@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.4053.1364815917.2939.python-list@python.org> (permalink)

Show all headers | View raw


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.  ])

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web