Re: looping through a list of lists.

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?

Rob

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.
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Advertisements

Rob Hunter wrote:
> 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?

I you are referring to the zip trick posted by ,
it *does work for an arbitrary number of sublists, but not for
arbitrary-length sublists:
>>> for loc in zip(*"alpha beta gamma delta".split()):
.... print loc
....
('a', 'b', 'g', 'd')
('l', 'e', 'a', 'e')
('p', 't', 'm', 'l')
('h', 'a', 'm', 't')
>>>

Advertisements

Peter Otten wrote:
> Rob Hunter wrote:
>
>
>>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?
>
>
> I you are referring to the zip trick posted by ,
> it *does work for an arbitrary number of sublists, but not for
> arbitrary-length sublists:
>
>
>>>>for loc in zip(*"alpha beta gamma delta".split()):
>
> ... print loc
> ...
> ('a', 'b', 'g', 'd')
> ('l', 'e', 'a', 'e')
> ('p', 't', 'm', 'l')
> ('h', 'a', 'm', 't')
>
>
> Peter

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!