Just a place where to dump some code to the community.
>>> x = [5,6,5]
>>> x_nodups = dict(map(lambda i: (i,1),x)).keys()
The fastest way to uniqify a list in Python is:>>> {}.fromkeys(x_nodups).keys()Time ago I checked a site with several ways to do this, and this was the chosen ( http://www.peterbe.com/plog/uniqifiers-benchmark )
Jacen's code:import randoma = []for i in range(0,1000000): a.append(int(random.random()*100))print dict(map(lambda i: (i,1), a)).keys()real 0m2.104suser 0m2.000ssys 0m0.090sHenhiskan's code:import randoma = []for i in range(0,1000000): a.append(int(random.random()*100))print {}.fromkeys(a).keys()real 0m0.838suser 0m0.770ssys 0m0.020sMy version:import randoma = []for i in range(0,1000000): a.append(int(random.random()*100))print list(set(a))real 0m0.734suser 0m0.690ssys 0m0.040sSeems faster and less obscure.Saludos
The fastest way to uniqify a list in Python is:
ResponderEliminar>>> {}.fromkeys(x_nodups).keys()
Time ago I checked a site with several ways to do this, and this was the chosen ( http://www.peterbe.com/plog/uniqifiers-benchmark )
Jacen's code:
ResponderEliminarimport random
a = []
for i in range(0,1000000): a.append(int(random.random()*100))
print dict(map(lambda i: (i,1), a)).keys()
real 0m2.104s
user 0m2.000s
sys 0m0.090s
Henhiskan's code:
import random
a = []
for i in range(0,1000000): a.append(int(random.random()*100))
print {}.fromkeys(a).keys()
real 0m0.838s
user 0m0.770s
sys 0m0.020s
My version:
import random
a = []
for i in range(0,1000000): a.append(int(random.random()*100))
print list(set(a))
real 0m0.734s
user 0m0.690s
sys 0m0.040s
Seems faster and less obscure.
Saludos