Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > index nested lists

Reply
Thread Tools

index nested lists

 
 
Alex
Guest
Posts: n/a
 
      07-28-2009
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 = 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
 
Reply With Quote
 
 
 
 
Andreas Tawn
Guest
Posts: n/a
 
      07-28-2009
> 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 = 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
 
Reply With Quote
 
 
 
 
Alex
Guest
Posts: n/a
 
      07-28-2009
On 28 Lug, 15:12, "Andreas Tawn" <(E-Mail Removed)> 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 = 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
 
Reply With Quote
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      07-28-2009
Alex wrote:
> On 28 Lug, 15:12, "Andreas Tawn" <(E-Mail Removed)> 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 = 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 )

JM
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" camelean@shaw.ca ASP .Net 3 02-22-2011 07:06 PM
locate items in matrix (index of lists of lists) Alexzive Python 6 03-20-2009 06:14 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
problem with index.html .(page is automatically gettin redirected to index.html) karthikeyavenkat Java 2 03-17-2005 10:01 PM



Advertisments