OVH Cloud OVH Cloud

sort -u

2 réponses
Avatar
George Abitbol
Bonjour,

je chercher une fonction built-in =E9quivalente =E0 sort -u pour
=E9liminer les doublons dans un tableau.

Merci

2 réponses

Avatar
Damien Wyart
* "George Abitbol" in fr.comp.lang.python:
je chercher une fonction built-in équivalente à sort -u pour éliminer
les doublons dans un tableau.


Si la conservation de l'ordre n'est pas importante :

def uniq(alist):
return list(set(alist))

Sinon :

def uniq(alist)
set = {}
return [set.setdefault(e,e) for e in alist if e not in set]

Pour des détails techniques complémentaires,
http://groups.google.fr/group/comp.lang.python/browse_thread/thread/32e545ebba11dd4d/
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560/


--
DW

Avatar
George Abitbol
Damien Wyart wrote:

def uniq(alist):
return list(set(alist))


Impeccable.

Merci.