Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99211
| Path | csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Laura Creighton <lac@openend.se> |
| Newsgroups | comp.lang.python |
| Subject | Re: Problem to read from array |
| Date | Sat, 21 Nov 2015 15:22:55 +0100 |
| Lines | 72 |
| Message-ID | <mailman.37.1448115786.2291.python-list@python.org> (permalink) |
| 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 | <lac@openend.se> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:99211 |
Show key headers only | View raw
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 "<stdin>", line 2, in <module>
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Problem to read from array vostrushka@gmail.com - 2015-11-21 02:41 -0800
Re: Problem to read from array Peter Otten <__peter__@web.de> - 2015-11-21 12:23 +0100
Re: Problem to read from array Laura Creighton <lac@openend.se> - 2015-11-21 15:22 +0100
Re: Problem to read from array Laura Creighton <lac@openend.se> - 2015-11-21 16:05 +0100
Re: Problem to read from array BartC <bc@freeuk.com> - 2015-11-21 15:26 +0000
Re: Problem to read from array Nathan Hilterbrand <nhilterbrand@gmail.com> - 2015-11-21 10:52 -0500
Re: Problem to read from array Crane Ugly <vostrushka@gmail.com> - 2015-11-23 12:58 -0800
Re: Problem to read from array sohcahtoa82@gmail.com - 2015-11-23 16:44 -0800
Re: Problem to read from array Crane Ugly <vostrushka@gmail.com> - 2015-11-24 00:56 -0800
Re: Problem to read from array Peter Otten <__peter__@web.de> - 2015-11-24 10:31 +0100
csiph-web