Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Laura Creighton Newsgroups: comp.lang.python Subject: Re: Problem to read from array Date: Sat, 21 Nov 2015 15:22:55 +0100 Lines: 72 Message-ID: References: <2141d7ae-1064-49f3-bf36-ff663fca0ffc@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: news.uni-berlin.de gEAT0g3IwyhkDBzIB1VhyQjwCCmYNcE6KUSV/uanQ0Fg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '[],': 0.07; 'append': 0.07; 'assignment': 0.07; 'expressions': 0.07; 'cc:addr:python- list': 0.09; '[0,': 0.09; 'received:openend.se': 0.09; 'received:theraft.openend.se': 0.09; 'python': 0.10; 'index': 0.13; '0],': 0.16; '[[],': 0.16; 'after,': 0.16; 'cc:addr:lac': 0.16; 'cc:addr:openend.se': 0.16; 'from:addr:lac': 0.16; 'from:addr:openend.se': 0.16; 'from:name:laura creighton': 0.16; 'happily': 0.16; 'indexerror:': 0.16; 'loops': 0.16; 'message- id:@fido.openend.se': 0.16; 'received:fido': 0.16; 'received:fido.openend.se': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Problem': 0.16; 'subject:array': 0.16; 'url:tutor': 0.16; 'laura': 0.18; '>>>': 0.20; 'cc:addr:python.org': 0.20; 'cc:2**1': 0.22; '"",': 0.22; 'arrays': 0.22; 'trying': 0.22; 'bit': 0.23; 'errors': 0.23; 'this:': 0.23; '(most': 0.24; 'this.': 0.28; 'received:se': 0.29; 'cc:no real name:2**1': 0.29; 'too.': 0.30; 'url:mailman': 0.30; 'work.': 0.30; 'point': 0.33; 'useful': 0.33; 'url:python': 0.33; 'traceback': 0.33; 'values.': 0.33; 'url:listinfo': 0.34; 'file': 0.34; 'lists': 0.34; 'list': 0.34; 'something': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'url:org': 0.36; 'subject:: ': 0.37; 'there,': 0.37; 'charset:us-ascii': 0.37; 'things': 0.38; "won't": 0.38; 'mailing': 0.38; 'does': 0.39; 'subject:from': 0.39; 'url:mail': 0.40; 'build': 0.40; 'where': 0.40; 'your': 0.60; 'hope': 0.61; 'header:Message-Id:1': 0.61; 'telling': 0.61; 'discuss': 0.61; 'here.': 0.62; 'skip:n 10': 0.62; 'more': 0.63; 'tutor': 0.66; 'news': 0.68; 'useful.': 0.72; 'hardly': 0.84; 'header:In-reply-to:1': 0.84; 'otten': 0.84; 'subject:read': 0.84; 'do:': 0.91; 'write:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openend.se; s=default; t=1448115776; bh=cVabhV5ajVHIJFM0lnSck96xFFLwUyWI/MlKmk3eEm4=; h=To:cc:From:Subject:In-reply-to:References:Date:From; b=uQKApGiwCi5qthWfsstPWG3YFnDxualTPfD33pmycrmPgBvUUzXyvOVkqm+u+SK1x P90Y21yA6qMlAYPdhcQCWlr3jvVWqlRoW6RblZjJI8MPwoJ8o9NTcbRnFH2/Hjbdvg rsg4qzrflqMnhJTbxpt11IbN3kPv06TjYBGQLOQw= In-reply-to: <2141d7ae-1064-49f3-bf36-ff663fca0ffc@googlegroups.com> Comments: In-reply-to vostrushka@gmail.com message dated "Sat, 21 Nov 2015 02:41:57 -0800." Content-ID: <32393.1448115775.1@fido> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.3.9 (theraft.openend.se [82.96.5.2]); Sat, 21 Nov 2015 15:22:56 +0100 (CET) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99211 In Python we don't write while loops that often. instead we do: for i in range(NumberOfColumns): for j in range(NumberOfRows): do_something() But for the particular case that you are after, constructing lists, we have something even neater -- list comprehensions. So you might write: >>> newarray=[] >>> for i in range(3): ... newarray[i] = i And this would not work. Traceback (most recent call last): File "", line 2, in IndexError: list assignment index out of range At this point you might get a bit frustrated. Python is happily telling you that you don't have a newarray[0][0] which is hardly news to you who was trying to initialise the thing. The trick is to initialise it with a list comprehension. You write one like this: [ calculate_something() for i in range(LIMIT)] This works: >>> newarray = [i for i in range(3)] >>> newarray [0, 1, 2] >>> As does >>> newarray = ["hello" for i in range(3)] >>> newarray ['hello', 'hello', 'hello'] You can even build your lists with a condition: >>> even_numbers=[x for x in range(11) if x % 2 == 0] >>> even_numbers [0, 2, 4, 6, 8, 10] You are not limited to one dimentional arrays (lists) here. You can have much more complicated expressions as the calculate_something. >>> newarray =[[ [x+2000, y*y] for x in range(3)] for y in range(5)] >>> newarray [[[2000, 0], [2001, 0], [2002, 0]], [[2000, 1], [2001, 1], [2002, 1]], [[2000, 4], [2001, 4], [2002, 4]], [[2000, 9], [2001, 9], [2002, 9]], [[2000, 16], [2001, 16], [2002, 16]]] Which leads us to a particularly useful construct: >>> emptyarray =[[ [] for x in range(3)] for y in range(5)] >>> emptyarray [[[], [], []], [[], [], []], [[], [], []], [[], [], []], [[], [], []]] Bingo. Now you won't have any index errors when you try to append to your x,y values. Hope this is useful. Also, there is the tutor mailing list https://mail.python.org/mailman/listinfo/tutor which you might be interested in, where we discuss things like this. Peter Otten is there, too. Laura