[Tutor] check for nested sequences & flatten
Thu, 14 Feb 2002 16:37:44 +0900
I am trying to test for nested lists and then flatten them if need be. flatten(seq) works fine (though I wonder if this is the most efficient way to go about it --I always wondered why flatten wasn't a sequence built-in method). But here my isNested func seems broken.
>>> isNested([1, , [3, ]])
# 0 is clearly wrong.
"""flatten: completely eliminate nesting of any sequence type"""
res = 
for item in seq:
if type(item) in (TupleType, ListType):
# what's up with this ?
"""isNested: checks a list for nesting and if nested, exactly how deeply
>>> isNested([1,2,3]) yeilds: 1
>>> isNested([1, , [3, ]]) yeilds: 3"""
depths = 
for obj in seq:
# as i sure to be over my quota of questions for the day, i'll go away now.
Love is in the Air!
Check out Cupid School where you will learn from Matchmaker's
best and brightest.
At Cupid School you'll learn how to pair up hopeful romantics
with one another, based on their personal preferences.
If you do well- you'll be rewarded with your own
set of love bow and arrows!