looping through a list of lists.

SBrunning at trisystems.co.uk SBrunning at trisystems.co.uk
Mon Oct 13 08:17:22 EDT 2003


	> On Wednesday, October 8, 2003, at 11:04 AM, saoirse_79 wrote:

	> 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.

> From:	Rob Hunter [SMTP:rob at cs.brown.edu]
> 
> The responder to this post has a cool way of doing it, but it won't 
> work (I believe) if you have an arbitrary number of sublists.  Can you 
> clarify the problem?  Do you have an arbitrary number of sublists?  Or 
> is it always 3?
 
It *can* work for arbitrary numbers of sublists, depending upon what one
means by 'comparing'. For example:

>>> spam = [['-', '-', '-', '-', '-', '-', '-', 'K', 'S', 'A', 'K'], ['-',
'-', '-', '-', 'L', 'Q', 'Q', 'T', 'N', 'S', 'E'], ['T', 'L', 'E', 'E', 'L',
'M', 'K', 'L', 'S', 'P', 'E']]
>>> for sublist in zip(*spam):
... 	print sublist, max(sublist), min(sublist)
... 
('-', '-', 'T') T -
('-', '-', 'L') L -
('-', '-', 'E') E -
('-', '-', 'E') E -
('-', 'L', 'L') L -
('-', 'Q', 'M') Q -
('-', 'Q', 'K') Q -
('K', 'T', 'L') T K
('S', 'N', 'S') S N
('A', 'S', 'P') S A
('K', 'E', 'E') K E
>>> spam.append(['1', '@', '_', 'Z', 'A', 'z', 'a', 'T', 'L', '5', '!'])
>>> for sublist in zip(*spam):
... 	print sublist, max(sublist), min(sublist)
... 
('-', '-', 'T', '1') T -
('-', '-', 'L', '@') L -
('-', '-', 'E', '_') _ -
('-', '-', 'E', 'Z') Z -
('-', 'L', 'L', 'A') L -
('-', 'Q', 'M', 'z') z -
('-', 'Q', 'K', 'a') a -
('K', 'T', 'L', 'T') T K
('S', 'N', 'S', 'L') S L
('A', 'S', 'P', '5') S 5
('K', 'E', 'E', '!') K !

Cheers,
Simon Brunning,
http://www.brunningonline.net/simon/blog/
--LongSig




-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.





More information about the Python-list mailing list