Outils pour utilisateurs

Outils du site


python:programmes:sort_array

Trier un tableau

Tous les exemples : https://wiki.python.org/moin/HowTo/Sorting

>>> Lf = ['ACBD', 'AABB', 'ABBB','ACBD','ACBD','ABBB']
>>> Li = ['ACBD', 'AAAB', 'AABB','ACBD','ACBD','AABB']
>>> Ltemp = Li+ Lf
>>> Lt = sorted([(x, Ltemp.count(x)) for x in set(Ltemp)])
>>> print Lt
[('AAAB', 1), ('AABB', 3), ('ABBB', 2), ('ACBD', 6)]

Trier par indices :

>>> print Lt
[('AAAB', 1), ('AABB', 3), ('ABBB', 2), ('ACBD', 6)]
>>> Lt.sort(key=operator.itemgetter(1), reverse=True)
>>> print Lt
[('ACBD', 6), ('AABB', 3), ('ABBB', 2), ('AAAB', 1)]

Eliminer les doublons :

>>> Lf = ['ACBD', 'AABB', 'ABBB','ACBD','ACBD','ABBB']
>>> Li = ['ACBD', 'AAAB', 'AABB','ACBD','ACBD','AABB']
>>> Ltemp = Li+ Lf
>>> Ltemp
['ACBD', 'AAAB', 'AABB', 'ACBD', 'ACBD', 'AABB', 'ACBD', 'AABB', 'ABBB', 'ACBD', 'ACBD', 'ABBB']
>>> set(Ltemp)
set(['AABB', 'AAAB', 'ABBB', 'ACBD'])
python/programmes/sort_array.txt · Dernière modification : 2014/02/21 23:34 de root