Re: looping through a list of lists.

Discussion in 'Python' started by Rob Hunter, Oct 8, 2003.

  1. Rob Hunter

    Rob Hunter Guest

    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
    >
     
    Rob Hunter, Oct 8, 2003
    #1
    1. Advertising

  2. Rob Hunter

    Peter Otten Guest

    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
     
    Peter Otten, Oct 8, 2003
    #2
    1. Advertising

  3. Rob Hunter

    anton muhin Guest

    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


    Consider the following:

    PythonWin 2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)] on win32.
    Portions Copyright 1994-2001 Mark Hammond () -
    see 'Help/About PythonWin' for further copyright information.
    >>> l = [range(0, 5), range(1, 7), range(3, 5)]
    >>> l

    [[0, 1, 2, 3, 4], [1, 2, 3, 4, 5, 6], [3, 4]]
    >>> for e in zip(*l):

    .... print e
    ....
    (0, 1, 3)
    (1, 2, 4)
    >>> for e in map(None, *l): print e

    ....
    (0, 1, 3)
    (1, 2, 4)
    (2, 3, None)
    (3, 4, None)
    (4, 5, None)
    (None, 6, None)
    >>>


    It pads missing elements with Nones. Is it what you need?

    zip(*l) is similar to zip(l[0], l[1], ...., l[len(l) - 1])---it
    substitutes list elements to function parameters.

    HTH,
    anton.
     
    anton muhin, Oct 8, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. saoirse_79

    looping through a list of lists.

    saoirse_79, Oct 8, 2003, in forum: Python
    Replies:
    0
    Views:
    256
    saoirse_79
    Oct 8, 2003
  2. saoirse_79

    Re: looping through lists

    saoirse_79, Oct 9, 2003, in forum: Python
    Replies:
    3
    Views:
    287
    Alex Martelli
    Oct 9, 2003
  3. Joachim Worringen
    Replies:
    6
    Views:
    299
  4. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    425
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  5. Andy
    Replies:
    3
    Views:
    493
    James Kanze
    Jun 8, 2007
Loading...

Share This Page