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
return result
[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 =
[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
not mine ;) (credit goes
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 like to take credit for these, but
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 like to take credit for these, but
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
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 stdin, line 1, in ?
TypeError: sum() can't sum strings [use ''.join(seq) instead]
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 attractive
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
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 reason I
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 20100200500 1000
flatten1104.499199.699854.301 1673.102 4084.301 8078.504
flatten2111.103204.706944.901 1778.793 4554.701 8773.494
Another try:
def flatten6(x, y):
return list(chain(*izip(x, y)))
(any case, this is shorter ;-)
Cyril
On 1/12/06, Michael Spencer [EMAIL PROTECTED] wrote:
Tim Hochberg wrote:
Michael Spencer wrote:
Robin Becker schrieb:
Is there some smart/fast way to flatten a level one list
Paul Rubin wrote:
Paul Rubin http://[EMAIL PROTECTED] 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 could be a contender for
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 you
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 for duck
Robin Becker wrote:
Paul Rubin wrote:
Paul Rubin http://[EMAIL PROTECTED] 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
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
--
[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,]
Here's one way:
d = [('x0
In article [EMAIL PROTECTED],
Paul Rubin http://[EMAIL PROTECTED] 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) - value
If you're
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
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]
result[2*i+1] =
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)))
well, i
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):
File stdin, line
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 timeit
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...
sum([a,b,c], )
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 Murman'''
return
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,y2
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,y1,] or
g([x0,x1,x2
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,y0,x1,y1,] or
g([x0,x1,x2
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
Paul Rubin http://[EMAIL PROTECTED] 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
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 additional
contenders that time faster
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
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
34 matches
Mail list logo