[Python-3000] [Python-Dev] Iterable String Redux (aka StringABC)

Ron Adam rrr at ronadam.com
Fri May 30 17:50:08 CEST 2008


Raymond Hettinger wrote:
> "Jim Jewett"
>> It isn't really stringiness that matters, it is that you have to
>> terminate even though you still have an iterable container.
>> Well said.
>>>> Guido had at least a start in Searchable, back when ABC
>> were still in the sandbox:
>> Have to disagree here. An object cannot know in general
> whether a flattener wants to split it or not. That is an application
> dependent decision. A better answer is be able to tell the
> flattener what should be considered atomic in a given circumstance.
>>> Raymond

A while back (a couple of years I think), we had a discussion on 
python-list about flatten in which I posted the following version of a 
flatten function. It turned out to be nearly twice as fast as any other 
version.
def flatten(L):
 """ Flatten a list in place. """
 i = 0
 while i < len(L):
 while type(L[i]) is list:
 L[i:i+1] = L[i]
 i += 1
 return L
For this to work the object to be flattened needs to be both mutable and 
list like. At the moment I can't think of any reason I would want to 
flatten anything that was not list like.
To make it a bit more flexible it could be changed just a bit.
def flatten(L):
 """ Flatten a list in place. """
 objtype = type(L)
 i = 0
 while i < len(L):
 while type(L[i]) is objtype:
 L[i:i+1] = L[i]
 i += 1
 return L
Generally, I don't think you would want to flatten dissimilar objects.
Cheers,
 Ron


More information about the Python-3000 mailing list

AltStyle によって変換されたページ (->オリジナル) /