A query about list
Mel Wilson
mwilson at the-wire.com
Fri Oct 10 11:05:23 EDT 2003
In article <slrnbobkr7.io6.ramen at lackingtalent.com>,
Dave Benjamin <ramen at lackingtalent.com> wrote:
>This *seems* to work, but I have a sneaking feeling there's a bug in here as
>a result of the slice assignment. I haven't been able to find a boundary
>case that breaks this, but it seems like the slice assignment would mess up
>the indexes in the loop. Can anyone find a way to either break or improve
>this?
It does or doesn't, depending on what you want:
def flatten (a):
for i in xrange (len(a)-1, -1, -1):
if type (a[i]) is type ([]):
a[i:i+1] = a[i]
def deep_flatten (a):
i = 0
while i < len (a):
if type (a[i]) is type ([]):
a[i:i+1] = a[i]
else:
i += 1
if __name__ == '__main__':
def test (xx):
print xx
x = xx[:]
flatten (x); print x
x = xx[:]
deep_flatten (x); print x
print
test ([1,2, [3,4], 5, [], [6,7,8], 9])
test ([1, [20,21], [30,[310,311],32], [], [4], 5])
Regards. Mel.
More information about the Python-list
mailing list