index nested lists

Discussion in 'Python' started by Alex, Jul 28, 2009.

  1. Alex

    Alex Guest

    hi at all,
    If I have this list:

    >>> lista

    ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]]

    if I want enumerate elements...I can see:

    >>> for parola in lista:

    print lista
    i = i + 1

    ciao
    1
    ['mela', 'pera', 'banana']
    [1, 2, 3]
    >>>


    but, if I want to enumerate elements about nested lists ??, something
    like:

    ciao
    1
    mela
    pera
    banana
    1
    2
    3

    ....How can I do ??

    Alex
     
    Alex, Jul 28, 2009
    #1
    1. Advertising

  2. Alex

    Andreas Tawn Guest

    > hi at all,
    > If I have this list:
    >
    > >>> lista

    > ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]]
    >
    > if I want enumerate elements...I can see:
    >
    > >>> for parola in lista:

    > print lista
    > i = i + 1
    >
    > ciao
    > 1
    > ['mela', 'pera', 'banana']
    > [1, 2, 3]
    > >>>

    >
    > but, if I want to enumerate elements about nested lists ??, something
    > like:
    >
    > ciao
    > 1
    > mela
    > pera
    > banana
    > 1
    > 2
    > 3
    >
    > ...How can I do ??
    >
    > Alex
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    You could do something like this.

    def printNestedList(lst):
    if isinstance(lst, list):
    for element in lst:
    printNestedList(element)
    else:
    print lst

    myList = ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]]
    printNestedList(myList)

    >>>

    ciao
    1
    mela
    pera
    banana
    1
    2
    3

    Cheers, Drea
     
    Andreas Tawn, Jul 28, 2009
    #2
    1. Advertising

  3. Alex

    Alex Guest

    On 28 Lug, 15:12, "Andreas Tawn" <> wrote:
    > > hi at all,
    > >  If I have this list:

    >
    > > >>> lista

    > > ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]]

    >
    > > if I want enumerate elements...I can see:

    >
    > > >>> for parola in lista:

    > >    print lista
    > >    i = i + 1

    >
    > > ciao
    > > 1
    > > ['mela', 'pera', 'banana']
    > > [1, 2, 3]

    >
    > > but, if I want to enumerate elements about nested lists ??, something
    > > like:

    >
    > > ciao
    > > 1
    > > mela
    > > pera
    > > banana
    > > 1
    > > 2
    > > 3

    >
    > > ...How can I do ??

    >
    > > Alex
    > > --
    > >http://mail.python.org/mailman/listinfo/python-list

    >
    > You could do something like this.
    >
    > def printNestedList(lst):
    >     if isinstance(lst, list):
    >         for element in lst:
    >             printNestedList(element)
    >     else:
    >         print lst
    >
    > myList = ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]]
    > printNestedList(myList)
    >



    thanks a lot !

    Alex
     
    Alex, Jul 28, 2009
    #3
  4. Alex wrote:
    > On 28 Lug, 15:12, "Andreas Tawn" <> wrote:
    >
    >>> hi at all,
    >>> If I have this list:
    >>>
    >>>>>> lista
    >>>>>>
    >>> ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]]
    >>>
    >>> if I want enumerate elements...I can see:
    >>>
    >>>>>> for parola in lista:
    >>>>>>
    >>> print lista
    >>> i = i + 1
    >>>
    >>> ciao
    >>> 1
    >>> ['mela', 'pera', 'banana']
    >>> [1, 2, 3]
    >>>
    >>> but, if I want to enumerate elements about nested lists ??, something
    >>> like:
    >>>
    >>> ciao
    >>> 1
    >>> mela
    >>> pera
    >>> banana
    >>> 1
    >>> 2
    >>> 3
    >>>
    >>> ...How can I do ??
    >>>
    >>> Alex
    >>> --
    >>> http://mail.python.org/mailman/listinfo/python-list
    >>>

    >> You could do something like this.
    >>
    >> def printNestedList(lst):
    >> if isinstance(lst, list):
    >> for element in lst:
    >> printNestedList(element)
    >> else:
    >> print lst
    >>
    >> myList = ['ciao', 1, ['mela', 'pera', 'banana'], [1, 2, 3]]
    >> printNestedList(myList)
    >>
    >>

    >
    >
    > thanks a lot !
    >
    > Alex
    >

    One hidden suggestion in Andreas answer is to write your code in
    english, if you can :eek:)

    JM
     
    Jean-Michel Pichavant, Jul 28, 2009
    #4
    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. karthikeyavenkat
    Replies:
    2
    Views:
    615
    Bryce
    Mar 17, 2005
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    413
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. Alexzive
    Replies:
    6
    Views:
    607
    Chris Rebert
    Mar 20, 2009
  4. Shawn W_
    Replies:
    5
    Views:
    294
    Aldric Giacomoni
    Sep 16, 2009
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    315
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page