Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '[0,': 0.09; '[0]': 0.09; 'nice!': 0.09; 'python:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; 'python': 0.11; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'something': 0.35; 'subject:?': 0.36; 'thank': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:n 10': 0.64; 'subject:this': 0.83; 'ana': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: How to do this? Date: Mon, 01 Apr 2013 13:32:29 +0200 Organization: None References: <83a54022-576d-465b-b6bd-7e21cde59d99@googlegroups.com> <29fa109e-8ed0-4b1b-a9c0-473e2bace687@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084939b.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364815917 news.xs4all.nl 6852 [2001:888:2000:d::a6]:48346 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42455 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. ])