Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #36463
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.021 |
| X-Spam-Evidence | '*H*': 0.96; '*S*': 0.00; '(0,': 0.09; 'compact': 0.09; 'iterate': 0.09; 'notation': 0.09; "(i'm": 0.16; '11:19': 0.16; 'col': 0.16; 'decent': 0.16; 'failed.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'pairs': 0.16; 'row': 0.16; 'storing': 0.16; 'work.)': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'memory': 0.18; 'bit': 0.21; 'explicit': 0.22; 'elements': 0.23; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'probably': 0.29; 'code': 0.31; 'system,': 0.32; 'could': 0.32; 'print': 0.32; 'subject:List': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'doing': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'subject: (': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'performance': 0.39; 'to:addr:python.org': 0.39; 'called': 0.39; 'list,': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'range': 0.60; 'first': 0.61; 'covers': 0.65; 'million': 0.72; 'subject:this': 0.84; '(ie': 0.84; '2013': 0.84; 'off,': 0.84; 'subject:Best': 0.91; 'hundred': 0.95 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=xXuVoZXEPWjyKpI0apNPzboJ02113S2JUYwO20CvsFY=; b=r69UFlQLMQAwBwRvikZFmRKrU1ee/j5rZV31JeM84W3QXYtAn+dHtG237VvchZP6NE PVL/Zz4Uwjc2mHw9QXsQSru4OobVO2Qch+nEMjAuDUk1pkW4V143Ptncb8x6+1JMXNHU msTLylt4twDCACLqYo59XPYhKFHfspAG2cqxUjK5Nc2XxWoBQf39SaRku6eHBeLCpbsi ueWXMDKRPPPfZyYanuejN1qAyqD3p+VZNay8hmExsq5zTXIJ4UrRKy7FMMjudph0F2tQ H2c/gzVz/n7Khap2FV7cR8bX3SjC26XCux6jE1EMhI5nyv6et9j/6epRexRn2N9MxRU/ 6Xbg== |
| MIME-Version | 1.0 |
| In-Reply-To | <25c1f151-7323-42de-833f-15639b6eff71@googlegroups.com> |
| References | <25c1f151-7323-42de-833f-15639b6eff71@googlegroups.com> |
| Date | Wed, 9 Jan 2013 13:06:05 +1100 |
| Subject | Re: Best way to do this? List loop (matrix?) iteration |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.300.1357697174.2939.python-list@python.org> (permalink) |
| Lines | 28 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1357697174 news.xs4all.nl 6858 [2001:888:2000:d::a6]:52555 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:36463 |
Show key headers only | View raw
On Wed, Jan 9, 2013 at 11:19 AM, <andydtaylor@gmail.com> wrote:
> stn_count = len(stn_list_short)
> for rowcount in range (0, stn_count):
> for colcount in range (0, stn_count):
> print stn_list_long[rowcount] stn_list_long[colcount]
First off, you can iterate over the list directly:
for row in stn_list_short:
for col in stn_list_short:
print row + col
(I'm not sure what your code was doing with the print line, because it
ought to have failed. Explicit concatenation will work.)
Secondly, you can make a list of all of those pairs with a compact
notation called a comprehension:
pairs = [row + col for row in stn_list_short for col in stn_list_short]
That covers your requirement #2, giving you a full list of all of
them. How big is k going to be? Storing the whole list in memory will
get a little awkward if you have very large k; on this system, I
started seeing performance issues with a thousand elements in the
list, but you could probably go to ten thousand (ie a hundred million
pairs) if you have a decent bit of RAM.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Best way to do this? List loop (matrix?) iteration andydtaylor@gmail.com - 2013-01-08 16:19 -0800
Re: Best way to do this? List loop (matrix?) iteration Mitya Sirenef <msirenef@lightbird.net> - 2013-01-08 19:33 -0500
Re: Best way to do this? List loop (matrix?) iteration Chris Angelico <rosuav@gmail.com> - 2013-01-09 13:06 +1100
Re: Best way to do this? List loop (matrix?) iteration andydtaylor@gmail.com - 2013-01-09 15:24 -0800
Re: Best way to do this? List loop (matrix?) iteration Dave Angel <d@davea.name> - 2013-01-09 18:49 -0500
Re: Best way to do this? List loop (matrix?) iteration andydtaylor@gmail.com - 2013-01-09 15:24 -0800
csiph-web