"Stringizing" a list
Mike Fletcher
mfletch at tpresence.com
Wed Aug 9 16:21:33 EDT 2000
Something like the following:
import types
allowedMap = { types.ListType:1, types.TupleType:1 }
You could also add classes that have "for" interfaces to get them expanded
along with the lists/tuples. Alternately, you could leave off the tuples if
you only want to expand lists.
Incidentally, here's my original version, which was much simpler :)
def collapse(inlist, type=type, ltype=types.ListType, maxint= sys.maxint):
'''
Destructively flatten a list hierarchy to a single level.
Non-recursive, and (as far as I can see, doesn't have any
glaring loopholes).
Further speedups and obfuscations by Tim Peters :)
'''
try:
# for every possible index
for ind in xrange( maxint):
# while that index currently holds a list
while type(inlist[ind]) is ltype:
# expand that list into the index (and
subsequent indicies)
inlist[ind:ind+1] = inlist[ind]
#ind = ind+1
except IndexError:
pass
return inlist
def collapse_safe(inlist):
'''
As collapse, but works on a copy of the inlist
'''
return collapse( inlist[:] )
Enjoy,
Mike
-----Original Message-----
From: Peter Schneider-Kamp [mailto:nowonder at nowonder.de]
Sent: Wednesday, August 09, 2000 5:43 PM
To: Mike Fletcher
Cc: 'cg at cdegroot.com'; Python Listserv (E-mail)
Subject: Re: "Stringizing" a list
Mike Fletcher wrote:
>
> Something like
> string.join(hyperCollapse( data))
>
> Should be fairly fast and general.
If it would work. What has to supplied as allowedmap?
hyperCollapse requires 2 parameters.
Peter
--
Peter Schneider-Kamp ++47-7388-7331
Herman Krags veg 51-11 mailto:peter at schneider-kamp.de
N-7050 Trondheim http://schneider-kamp.de
--
http://www.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list