Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python set type not sorted [duplicate]

Tags:

python

sorting

I have a very odd issue when trying to sort my permutations.

As I have seen before on SO, using a set to sort and distinct a list is the way to go, but for one particular list, this seems not to work. We can take this for example

>>> set([2,1])
{1, 2}
>>> set([1,2,1])
{1, 2}
>>> set([513, 135, 531, 153, 315, 351])
{513, 135, 531, 153, 315, 351}
>>> 

Anyone who can give me any guidance?

like image 714
Pax Vobiscum Avatar asked Sep 05 '25 03:09

Pax Vobiscum


1 Answers

Sets are by definition an unordered collection.

To get a sorted collection from a set use

sorted(set([2,1]))

https://docs.python.org/2/library/sets.html

like image 83
gnicholas Avatar answered Sep 07 '25 21:09

gnicholas