Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #36463
| References | <25c1f151-7323-42de-833f-15639b6eff71@googlegroups.com> |
|---|---|
| Date | 2013-01-09 13:06 +1100 |
| Subject | Re: Best way to do this? List loop (matrix?) iteration |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.300.1357697174.2939.python-list@python.org> (permalink) |
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