looping through a list of lists.

Andy Todd andy47 at halfcooked.com
Wed Oct 8 12:29:57 EDT 2003


SBrunning at trisystems.co.uk wrote:
>>From:	saoirse_79 [SMTP:saoirse_79 at yahoo.com]
>>I have a list of lists as follows:
>>[['-', '-', '-', '-', '-', '-', '-', 'K', 'S', 'A', 'K'],
>>['-', '-', '-', '-', 'L', 'Q', 'Q', 'T', 'N', 'S', 'E'], 
>>['T', 'L', 'E', 'E', 'L', 'M', 'K', 'L', 'S', 'P', 'E']]
>>I want to be able to read each character  and compare it with all 
>>characters at the same positon in all sublists. Is this possible.
>>I have tried a few different methods but nothing seems to allow me to 
>>compare the sublists character by character.
> 
>  
> PythonWin 2.3.2 (#49, Oct  2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on
> win32.
> Portions Copyright 1994-2001 Mark Hammond (mhammond at skippinet.com.au) - see
> 'Help/About PythonWin' for further copyright information.
> 
>>>>spam = [['-', '-', '-', '-', '-', '-', '-', 'K', 'S', 'A', 'K'],['-',
> 
> '-', '-', '-', 'L', 'Q', 'Q', 'T', 'N', 'S', 'E'], ['T', 'L', 'E', 'E', 'L',
> 'M', 'K', 'L', 'S', 'P', 'E']]
> 
>>>>for eggs, chips, beans in zip(*spam):
> 
> ... 	print eggs, chips, beans
> ... 
> - - T
> - - L
> - - E
> - - E
> - L L
> - Q M
> - Q K
> K T L
> S N S
> A S P
> K E E
> 
> HTH...
> 
> Cheers,
> Simon Brunning,
> http://www.brunningonline.net/simon/blog/
> --LongSig
> 

OK Simon, that is pretty evil. Please explain to a poor ignoramous the 
difference between zip(spam) and zip(*spam).

I can see that the first gives;

[(['-', '-', '-', '-', '-', '-', '-', 'K', 'S', 'A', 'K'],),
  (['-','-', '-', '-', 'L', 'Q', 'Q', 'T', 'N', 'S', 'E'],),
  (['T', 'L', 'E', 'E', 'L','M', 'K', 'L', 'S', 'P', 'E'],)]

and the second produces the result you illustrate above, but why?

Regards,
Andy
-- 
--------------------------------------------------------------------------------
 From the desk of Andrew J Todd esq - http://www.halfcooked.com/







More information about the Python-list mailing list