Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Best way to do this? List loop (matrix?) iteration

Reply
Thread Tools

Best way to do this? List loop (matrix?) iteration

 
 
andydtaylor@gmail.com
Guest
Posts: n/a
 
      01-09-2013
Hi!

I might be missing the obvious, or I may have found something more complicated than the VBA I am used to. Could it be I need to use a maths library?

For a given list of k items I'd like to turn it into an k*k matrix of item pairs.

List_sample = ['a', 'b', 'c']

Output:

aa ab ac
ba bb bc
ca cb cc

I'd like to have 2 hooks into this process
1. I want the opportunity to use a value pair each time they are generated (because I need to send these to an api and get a number back to put into a temporary list or dictionary - still tbd).
2. I'd also like to know each time a row is completed so I can bank that temporary list to a database table. Else build one big list and do it at the end, I'm still figuring this out.

#Code I've tried:

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]

I've found itertools, tee, and product and felt I was getting warmer. I'm still looking, but any pointers would be appreciated!

Thanks,

Andy


 
Reply With Quote
 
 
 
 
Mitya Sirenef
Guest
Posts: n/a
 
      01-09-2013
On Tue 08 Jan 2013 07:19:59 PM EST, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi!
>
> I might be missing the obvious, or I may have found something more complicated than the VBA I am used to. Could it be I need to use a maths library?
>
> For a given list of k items I'd like to turn it into an k*k matrix of item pairs.
>
> List_sample = ['a', 'b', 'c']
>
> Output:
>
> aa ab ac
> ba bb bc
> ca cb cc
>
> I'd like to have 2 hooks into this process
> 1. I want the opportunity to use a value pair each time they are generated (because I need to send these to an api and get a number back to put into a temporary list or dictionary - still tbd).
> 2. I'd also like to know each time a row is completed so I can bank that temporary list to a database table. Else build one big list and do it at the end, I'm still figuring this out.
>
> #Code I've tried:
>
> 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]
>
> I've found itertools, tee, and product and felt I was getting warmer. I'm still looking, but any pointers would be appreciated!
>
> Thanks,
>
> Andy
>
>



You can use itertools.product("abc", repeat=2) together with itertools
recipe grouper() from the same page:

http://docs.python.org/3.3/library/i...ools#itertools


HTH, -m


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
 
Reply With Quote
 
 
 
 
Chris Angelico
Guest
Posts: n/a
 
      01-09-2013
On Wed, Jan 9, 2013 at 11:19 AM, <(E-Mail Removed)> 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
 
Reply With Quote
 
andydtaylor@gmail.com
Guest
Posts: n/a
 
      01-09-2013
Thanks for your help - this is what I did - though it's probably obvious to most people reading this.

for rowcount in range (0, stn_count):
row_durations.append(stn_list_short[rowcount])
for colcount in range (0, stn_count):
# 3. Determine Station pairs for API query
query_origin_stop = stn_list_long[rowcount]
query_destination_stop = stn_list_long[colcount]
# 4. Paths for determining duration. "station x = station x" has journey time 0
# 4a. Stations are SAME


.....etc. and this part works! I am now stuck on something else though, but I'll start a new topic for that.

Thanks

Andy
 
Reply With Quote
 
andydtaylor@gmail.com
Guest
Posts: n/a
 
      01-09-2013
Thanks for your help - this is what I did - though it's probably obvious to most people reading this.

for rowcount in range (0, stn_count):
row_durations.append(stn_list_short[rowcount])
for colcount in range (0, stn_count):
# 3. Determine Station pairs for API query
query_origin_stop = stn_list_long[rowcount]
query_destination_stop = stn_list_long[colcount]
# 4. Paths for determining duration. "station x = station x" has journey time 0
# 4a. Stations are SAME


.....etc. and this part works! I am now stuck on something else though, but I'll start a new topic for that.

Thanks

Andy
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      01-09-2013
On 01/09/2013 06:24 PM, (E-Mail Removed) wrote:
> Thanks for your help - this is what I did - though it's probably obvious to most people reading this.
>
> for rowcount in range (0, stn_count):
> row_durations.append(stn_list_short[rowcount])
> for colcount in range (0, stn_count):
> # 3. Determine Station pairs for API query
> query_origin_stop = stn_list_long[rowcount]
> query_destination_stop = stn_list_long[colcount]
> # 4. Paths for determining duration. "station x = station x" has journey time 0
> # 4a. Stations are SAME
>

Please reread Chris Angelico's message. Iterating over the lists
themselves, instead of making a range, is shorter, easier to read, and
usually quicker.

> ....etc. and this part works! I am now stuck on something else though, but I'll start a new topic for that.
>
> Thanks
>
> Andy

Untested:

for rowshort, query_origin_stop in zip(stn_list_short, stn_list_long):
row_durations.append(rowshort)
for query_destination_stop in stn_list_long:
#4 . Determine ...

--

DaveA

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Try to exit inner loop and start next iteration of outter loop. Nene Perl Misc 6 12-13-2008 12:20 AM
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
Best way to loop through ArrayList and remove elements on the way? Kevin Java 16 01-30-2008 08:54 PM



Advertisments