flatten a level one list

Cyril Bazin cyril.bazin at info.unicaen.fr
Thu Jan 12 03:59:45 EST 2006


Another try:

def flatten6(x, y):
    return list(chain(*izip(x, y)))

(any case, this is shorter ;-)

Cyril

On 1/12/06, Michael Spencer <mahs at telcopartners.com> wrote:
> Tim Hochberg wrote:
> > Michael Spencer wrote:
> >>  > Robin Becker schrieb:
> >>  >> Is there some smart/fast way to flatten a level one list using the
> >>  >> latest iterator/generator idioms.
> >> ...
> >>
> >> David Murmann wrote:
> >>  > Some functions and timings
> >> ...
> >
> > Here's one more that's quite fast using Psyco, but only average without it.
> >
> >
> > def flatten6():
> >      n = min(len(xdata), len(ydata))
> >      result = [None] * (2*n)
> >      for i in xrange(n):
> >              result[2*i] = xdata[i]
> >              result[2*i+1] = ydata[i]
> >
> > -tim
> >
> Indeed:
>
> I added yours to the list (after adding the appropriate return)
>
>   >>> testthem()
>   >>> timethem()
>   flatten1(...)  702 iterations, 0.71msec per call
>   flatten2(...)  641 iterations, 0.78msec per call
>   flatten3(...)  346 iterations, 1.45msec per call
>   flatten4(...)  1447 iterations, 345.66usec per call
>   flatten5(...)  1218 iterations, 410.55usec per call
>   flatten6(...)  531 iterations, 0.94msec per call
>   >>>
>
> (See earlier post for flatten1-5)
>
> Michael
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list