[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: pairs from a list

Matthew_WARREN

1/23/2008 5:46:00 PM


I'm just fiddling with this, am no great expert, but I added

def pairs5(x):
o=[]
for n in zip(x[::2],x[1:2]):
o.append(n)
return o

I dont know if that breaks any constraints placed on the problem, but I get
the following output

0.07158942896 0.266009705575 0.21342143313 0.0537146193457 0.0107680502972


does that make it faster than pairs(4) or am I breaking some constraints on
the problem?



Matt.






Internet
aisaac@american.edu
To
python-list
Sent by: cc
python-list-bounces+matthew.warren=uk.bnpparibas.com@
python.org Subject
Re: pairs from a list
22/01/2008 18:09










Arnaud Delobelle wrote:
> pairs4 wins.


Oops. I see a smaller difference,
but yes, pairs4 wins.

Alan Isaac

import time
from itertools import islice, izip

x = range(500001)

def pairs1(x):
return izip(islice(x,0,None,2),islice(x,1,None,2))

def pairs2(x):
xiter = iter(x)
while True:
yield xiter.next(), xiter.next()

def pairs3(x):
for i in range( len(x)//2 ):
yield x[2*i], x[2*i+1],

def pairs4(x):
xiter = iter(x)
return izip(xiter,xiter)

t = time.clock()
for x1, x2 in pairs1(x):
pass
t1 = time.clock() - t

t = time.clock()
for x1, x2 in pairs2(x):
pass
t2 = time.clock() - t

t = time.clock()
for x1, x2 in pairs3(x):
pass
t3 = time.clock() - t

t = time.clock()
for x1, x2 in pairs4(x):
pass
t4 = time.clock() - t

print t1, t2, t3, t4

Output:
0.317524154606 1.13436847421 1.07100930426 0.262926712753
--
http://mail.python.org/mailman/listinfo/p...



This message and any attachments (the "message") is
intended solely for the addressees and is confidential.
If you receive this message in error, please delete it and
immediately notify the sender. Any use not in accord with
its purpose, any dissemination or disclosure, either whole
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message.
BNP PARIBAS (and its subsidiaries) shall (will) not
therefore be liable for the message if modified.
Do not print this message unless it is necessary,
consider the environment.

---------------------------------------------

Ce message et toutes les pieces jointes (ci-apres le
"message") sont etablis a l'intention exclusive de ses
destinataires et sont confidentiels. Si vous recevez ce
message par erreur, merci de le detruire et d'en avertir
immediatement l'expediteur. Toute utilisation de ce
message non conforme a sa destination, toute diffusion
ou toute publication, totale ou partielle, est interdite, sauf
autorisation expresse. L'internet ne permettant pas
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce
message, dans l'hypothese ou il aurait ete modifie.
N'imprimez ce message que si necessaire,
pensez a l'environnement.
1 Answer

Peter Otten

1/23/2008 6:32:00 PM

0

Matthew_WARREN wrote:

> I'm just fiddling with this, am no great expert, but I added
>
> def pairs5(x):
> o=[]
> for n in zip(x[::2],x[1:2]):

The second argument should be x[1::2].

> o.append(n)
> return o
>
> I dont know if that breaks any constraints placed on the problem, but I

It breaks the constraints unless the above is not cut-n-paste ;)
Also note that your pairs5() offers no advantage over

def pairs6(x):
return zip(x[::2], x[1::2])

Peter