Re: flatten a level one list

2006-01-13 Thread Cyril Bazin
I added my own function to the benchmark of Robin Becker:from itertools import chaindef flatten9(x, y):   return list(chain(*izip(x, y)))Results: no psyco Name   10   20  100  200  500 1000 flatten1  104.499  199.699  854.301 167

Re: flatten a level one list

2006-01-13 Thread Bengt Richter
On Fri, 13 Jan 2006 07:48:39 -0800, [EMAIL PROTECTED] (Alex Martelli) wrote: >Nick Craig-Wood <[EMAIL PROTECTED]> wrote: > ... >> I agree with Guido about the special case, but I disagree about the >> error message. Not being able to use sum(L,"") reduces the >> orthogonality of sum for no good

Re: flatten a level one list

2006-01-13 Thread Michael Spencer
> Michael Spencer wrote: >> result[ix::count] = input + [pad]*(maxlen-lengths[ix]) Peter Otten rewrote: > result[ix:len(input)*count:count] = input Quite so. What was I thinking? Michael -- http://mail.python.org/mailman/listinfo/python-list

Re: flatten a level one list

2006-01-13 Thread Alex Martelli
Nick Craig-Wood <[EMAIL PROTECTED]> wrote: ... > I agree with Guido about the special case, but I disagree about the > error message. Not being able to use sum(L,"") reduces the > orthogonality of sum for no good reason I could see. Having sum(L,'') work but be O(N squared) would be an "attrac

Re: flatten a level one list

2006-01-13 Thread Nick Craig-Wood
Alex Martelli <[EMAIL PROTECTED]> wrote: > Nick Craig-Wood <[EMAIL PROTECTED]> wrote: > > Except if you are trying to sum arrays of strings... > > > > >>> sum(["a","b","c"], "") > > Traceback (most recent call last): > > File "", line 1, in ? > > TypeError: sum() can't sum strings [use

Re: flatten a level one list

2006-01-13 Thread Robin Becker
Peter Otten wrote: .. > - You are padding twice -- once with None, and then with the real thing. > > def interleave2(*args, **kw): > dopad = "pad" in kw > pad = kw.get("pad") > count = len(args) > lengths = map(len, args) > maxlen = max(lengths) > if not dopad and

Re: flatten a level one list

2006-01-13 Thread bonono
Peter Otten wrote: > [EMAIL PROTECTED] wrote: > > > David Murmann wrote: > > >> > # New attempts: > >> > from itertools import imap > >> > def flatten4(x, y): > >> > '''D Murman''' > >> > l = [] > >> > list(imap(l.extend, izip(x, y))) > >> > return l > > >> well, i would really lik

Re: flatten a level one list

2006-01-13 Thread bonono
Peter Otten wrote: > [EMAIL PROTECTED] wrote: > > > David Murmann wrote: > > >> > # New attempts: > >> > from itertools import imap > >> > def flatten4(x, y): > >> > '''D Murman''' > >> > l = [] > >> > list(imap(l.extend, izip(x, y))) > >> > return l > > >> well, i would really lik

Re: flatten a level one list

2006-01-13 Thread Peter Otten
[EMAIL PROTECTED] wrote: > David Murmann wrote: >> > # New attempts: >> > from itertools import imap >> > def flatten4(x, y): >> > '''D Murman''' >> > l = [] >> > list(imap(l.extend, izip(x, y))) >> > return l >> well, i would really like to take credit for these, but they're >>

Re: flatten a level one list

2006-01-13 Thread Peter Otten
[EMAIL PROTECTED] wrote: >> Creating a list via list/map/filter just for the side effect is not only >> bad taste, >> >> ~ $ python -m timeit -s'a = zip([range(1000)]*2)' 'lst=[];ext=lst.extend' >> 'for i in a: ext(i)' >> 100 loops, best of 3: 1.23 usec per loop >> ~ $ python -m timeit -s'a =

Re: flatten a level one list

2006-01-13 Thread Peter Otten
Michael Spencer wrote: > Peter Otten wrote: > >> If you require len(xdata) == len(ydata) there's an easy way to move the >> loop into C: >> >> def flatten7(): >> n = len(xdata) >> assert len(ydata) == n >> result = [None] * (2*n) >> result[::2] = xdata >> result[1::2] = ydata

Re: flatten a level one list

2006-01-12 Thread bonono
David Murmann wrote: > Robin Becker schrieb: > > # New attempts: > > from itertools import imap > > def flatten4(x, y): > > '''D Murman''' > > l = [] > > list(imap(l.extend, izip(x, y))) > > return l > > > > > > from Tkinter import _flatten > > def flatten5(x, y): > > '''D Murm

Re: flatten a level one list

2006-01-12 Thread Alex Martelli
Nick Craig-Wood <[EMAIL PROTECTED]> wrote: > Sion Arrowsmith <[EMAIL PROTECTED]> wrote: > > sum(...) > > sum(sequence, start=0) -> value > > > > If you're using sum() as a 1-level flatten you need to give it > > start=[]. > > Except if you are trying to sum arrays of strings... > > >>

Re: flatten a level one list

2006-01-12 Thread Michael Spencer
Peter Otten wrote: > I you require len(xdata) == len(ydata) there's an easy way to move the loop > into C: > > def flatten7(): > n = len(xdata) > assert len(ydata) == n > result = [None] * (2*n) > result[::2] = xdata > result[1::2] = ydata > return result > > $ python -m

Re: flatten a level one list

2006-01-12 Thread Nick Craig-Wood
Sion Arrowsmith <[EMAIL PROTECTED]> wrote: > sum(...) > sum(sequence, start=0) -> value > > If you're using sum() as a 1-level flatten you need to give it > start=[]. Except if you are trying to sum arrays of strings... >>> sum(["a","b","c"], "") Traceback (most recent call last):

Re: flatten a level one list

2006-01-12 Thread David Murmann
Robin Becker schrieb: > # New attempts: > from itertools import imap > def flatten4(x, y): > '''D Murman''' > l = [] > list(imap(l.extend, izip(x, y))) > return l > > > from Tkinter import _flatten > def flatten5(x, y): > '''D Murman''' > return list(_flatten(zip(x, y)))

Re: flatten a level one list

2006-01-12 Thread Robin Becker
Peter Otten wrote: > Tim Hochberg wrote: > > >>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] >>

Re: flatten a level one list

2006-01-12 Thread Paul Rubin
Sion Arrowsmith <[EMAIL PROTECTED]> writes: > sum(sequence, start=0) -> value > > If you're using sum() as a 1-level flatten you need to give it > start=[]. Oh, right, I should have remembered that. Thanks. Figuring out whether it's quadratic or linear would still take an experiment or code

Re: flatten a level one list

2006-01-12 Thread Sion Arrowsmith
In article <[EMAIL PROTECTED]>, Paul Rubin wrote: >Robin Becker <[EMAIL PROTECTED]> writes: >> >reduce(operator.add,a) >> ... >That's what I hoped "sum" would do, but instead it barfs with a type >error. So much for duck typing. sum(...) sum(sequence, start=0)

Re: flatten a level one list

2006-01-12 Thread Raymond Hettinger
[Robin Becker] > Is there some smart/fast way to flatten a level one list using the > latest iterator/generator idioms. > > The problem arises in coneverting lists of (x,y) coordinates into a > single list of coordinates eg > > f([(x0,y0),(x1,y1),]) --> [x0,y0,x1,

Re: flatten a level one list

2006-01-12 Thread bearophileHUGS
Well, maybe it's time to add a n-levels flatten() function to the language (or to add it to itertools). Python is open source, but I am not able to modify its C sources yet... Maybe Raymond Hettinger can find some time to do it for Py 2.5. Bye, bearophile -- http://mail.python.org/mailman/listin

Re: flatten a level one list

2006-01-12 Thread bonono
Robin Becker wrote: > Paul Rubin wrote: > > Paul Rubin writes: > > > >import operator > >a=[(1,2),(3,4),(5,6)] > >reduce(operator.add,a) > >> > >>(1, 2, 3, 4, 5, 6) > > > > > > (Note that the above is probably terrible if the lists are large and > > you're aft

Re: flatten a level one list

2006-01-12 Thread Paul Rubin
Robin Becker <[EMAIL PROTECTED]> writes: > >reduce(operator.add,a) > ... > A fast implementation would probably allocate the output list just > once and then stream the values into place with a simple index. That's what I hoped "sum" would do, but instead it barfs with a type error. So much f

Re: flatten a level one list

2006-01-12 Thread Peter Otten
Tim Hochberg wrote: > 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] I

Re: flatten a level one list

2006-01-12 Thread Robin Becker
Paul Rubin wrote: > Paul Rubin writes: > >import operator >a=[(1,2),(3,4),(5,6)] >reduce(operator.add,a) >> >>(1, 2, 3, 4, 5, 6) > > > (Note that the above is probably terrible if the lists are large and > you're after speed.) yes, and it is all in C and so

Re: flatten a level one list

2006-01-12 Thread Cyril Bazin
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

Re: flatten a level one list

2006-01-11 Thread Michael Spencer
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

Re: flatten a level one list

2006-01-11 Thread Tim Hochberg
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

Re: flatten a level one list

2006-01-11 Thread Michael Spencer
> 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 are some more timings of David's functions, and a couple of additiona

Re: flatten a level one list

2006-01-11 Thread Paul Rubin
Paul Rubin writes: > >>> import operator > >>> a=[(1,2),(3,4),(5,6)] > >>> reduce(operator.add,a) > (1, 2, 3, 4, 5, 6) (Note that the above is probably terrible if the lists are large and you're after speed.) -- http://mail.python.org/mailman/listinfo/python-list

Re: flatten a level one list

2006-01-11 Thread Paul Rubin
Robin Becker <[EMAIL PROTECTED]> writes: > f([(x0,y0),(x1,y1),]) --> [x0,y0,x1,y1,] >>> import operator >>> a=[(1,2),(3,4),(5,6)] >>> reduce(operator.add,a) (1, 2, 3, 4, 5, 6) -- http://mail.python.org/mailman/listinfo/python-list

Re: flatten a level one list

2006-01-11 Thread David Murmann
Robin Becker schrieb: > Is there some smart/fast way to flatten a level one list using the > latest iterator/generator idioms. > > The problem arises in coneverting lists of (x,y) coordinates into a > single list of coordinates eg > > f([(x0,y0),(x1,y1),]) --> [x0,

Re: flatten a level one list

2006-01-11 Thread bonono
Robin Becker wrote: > Is there some smart/fast way to flatten a level one list using the > latest iterator/generator idioms. > > The problem arises in coneverting lists of (x,y) coordinates into a > single list of coordinates eg > > f([(x0,y0),(x1,y1),]) --> [x0,y0,x1,

flatten a level one list

2006-01-11 Thread Robin Becker
Is there some smart/fast way to flatten a level one list using the latest iterator/generator idioms. The problem arises in coneverting lists of (x,y) coordinates into a single list of coordinates eg f([(x0,y0),(x1,y1),]) --> [x0,y0,x1,y1,] or g([x0,x1,x2,..],[y0,y1