157

I have values like this:

set(['0.000000000', '0.009518000', '10.277200999', '0.030810999', '0.018384000', '4.918560000'])
set(['4.918859000', '0.060758000', '4.917336999', '0.003949999', '0.013945000', '10.281522000', '0.025082999'])  

I want to sort the values in each set in increasing order. I don't want to sort between the sets, but the values in each set.

abarnert
  • 334,953
  • 41
  • 559
  • 636
Justin Carrey
  • 3,231
  • 8
  • 30
  • 43
  • 3
    What do the tuples have to do with the problem and what have you tried? – Fred Foo Jul 03 '13 at 20:45
  • 4
    I don't think python sets have a concept of ordering. This might help you: http://stackoverflow.com/questions/1653970/does-python-have-an-ordered-set – zallarak Jul 03 '13 at 20:46
  • 3
    a set is orderless. that's one of it's properties. in every language. use a `list`. – Karoly Horvath Jul 03 '13 at 20:52
  • 1
    @JustinCarrey: Since the stuff about the dictionary is irrelevant, and referring to a set of values as a list of values is confusing, I've stripped all of that out of the question. If you don't think the edited version reflects your actual problem, please revert it (or, if you don't have enough rep, just ask me to do it for you). – abarnert Jul 03 '13 at 21:10
  • 5
    @KarolyHorvath "a set is orderless. that's one of it's properties. in every language." C++'s `std::set` is ordered. – Timothy Shields Jul 03 '13 at 21:24
  • 1
    @Timothy Shields: internally. a fixed order. that's a different concept. if you have a set, you cannot change the order of elements within it. all you can do is provide a comparison operator. – Karoly Horvath Jul 03 '13 at 22:14

1 Answers1

297

From a comment:

I want to sort each set.

That's easy. For any set s (or anything else iterable), sorted(s) returns a list of the elements of s in sorted order:

>>> s = set(['0.000000000', '0.009518000', '10.277200999', '0.030810999', '0.018384000', '4.918560000'])
>>> sorted(s)
['0.000000000', '0.009518000', '0.018384000', '0.030810999', '10.277200999', '4.918560000']

Note that sorted is giving you a list, not a set. That's because the whole point of a set, both in mathematics and in almost every programming language,* is that it's not ordered: the sets {1, 2} and {2, 1} are the same set.


You probably don't really want to sort those elements as strings, but as numbers (so 4.918560000 will come before 10.277200999 rather than after).

The best solution is most likely to store the numbers as numbers rather than strings in the first place. But if not, you just need to use a key function:

>>> sorted(s, key=float)
['0.000000000', '0.009518000', '0.018384000', '0.030810999', '4.918560000', '10.277200999']

For more information, see the Sorting HOWTO in the official docs.


* See the comments for exceptions.

abarnert
  • 334,953
  • 41
  • 559
  • 636
  • "That's because the whole point of a set, both in mathematics and in every programming language, is that it's not ordered" - C++'s `std::set` is ordered. – Timothy Shields Jul 03 '13 at 21:26
  • 1
    @TimothyShields: You're right, I should have said "every programming language that doesn't like to misuse common terms". In a language where "non-modifying" means "mutating", it's not surprising that `set` means "sorted list", and you have to say `unordered_set` to mean "set"… – abarnert Jul 03 '13 at 21:33
  • 2
    Whether that name is good or bad isn't what's up for discussion. The statement that "sets are never ordered in any language" is false. – Timothy Shields Jul 03 '13 at 21:36
  • 8
    You won't find the word "unordered" on the Wikipedia page about a mathematical set. You also won't find the word "ordered." That's because it's **irrelevant** to mathematical operations on sets. Whether a particular language chooses to make sets ordered is an implementation detail. http://en.wikipedia.org/wiki/Set_(mathematics) – Timothy Shields Jul 03 '13 at 21:38
  • @TimothyShields: The Wikipedia page on [Set (computer science)](http://en.wikipedia.org/wiki/Set_(computer_science)) says "In computer science, a set is an abstract data structure that can store certain values, without any particular order…". It doesn't list C++ among languages that support sets, and instead has a separate section on C++ set being a different thing. At any rate, I already edited the answer to say "almost every language", and link to the Wikipedia page, so I don't know what you're arguing about. – abarnert Jul 03 '13 at 21:40
  • 4
    @TimothyShields: Meanwhile, the wikipedia page on Set (mathematics) that you linked doesn't have the word "unordered" because it's already obvious from the definition. In particular, "Sets A and B are equal if and only if they have precisely the same elements". That's not true for ordered collections. (There are also sentences like: "The order in which the elements of a set or multiset are listed is irrelevant", so if you'd just searched a little differently you could have found it even without understanding the concept.) – abarnert Jul 03 '13 at 21:43
  • 1
    If a "set" interface does not expose ordering, then whether the "set" implementation uses ordering behind the scenes is irrelevant. In C++, I can make a `std::set` that is conceptually equivalent to `{2, 1, 5, 3}` - because the conceptual "set" definition doesn't involve ordering. – Timothy Shields Jul 03 '13 at 21:50
  • 4
    @TimothyShields: But `std::set` _does_ expose the ordering (not just in the obvious ways through the API; the ordering function is actually _part of the type_ of any given set). The fact that the elements are always kept sorted by a strict weak order is part of the very definition of the concept. So, your argument is moot. But even if it weren't, why would it matter? Sets are unordered in almost every programming language, just as they are in math. The fact that there's one outlying language that uses the word to mean something else doesn't make that statement any less true. – abarnert Jul 03 '13 at 23:11
  • "Sets A and B are equal if and only if they have precisely the same elements." Meanwhile some programming languages would consider the variables representing the sets equal only if they lead to the same reference... – Ferazhu May 05 '21 at 22:31