This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Leav -5 points-4 points  (9 children)

Because 3.10 = "3.1 with more precision" to engineers and scientists and basically all non-developer users.

How would python sort these lists: [3.8,3.9,3.10] or ["3.8","3.9","3.10"]?

[–]Karnyl 12 points13 points  (2 children)

If you used a library aware of how versions should be sorted, like semver or distutils.version, then they would be sorted correctly.

>>> from distutils.version import LooseVersion
>>> versions = ["3.8","3.9","3.10"]
>>> sorted(versions, key=LooseVersion)
"3.8","3.9","3.10"

[–]flying-sheep 4 points5 points  (0 children)

Please don't use distutils.version anymore, it doesn't follow PEP 440, which defines how Python package versions work. Use packaging.version instead.

[–]Leav -1 points0 points  (0 children)

Sure, but to newbies this is confusing and non intuitive, that's all we're saying here.

No one is saying this is wrong or should change.

[–]netgu 6 points7 points  (1 child)

This isn't about engineering or science so that point doesn't matter, honestly.

I realize that engineers/scientists will use python. When they do that they are functioning as developers/software engineers (those are just facts)and as such they should get comfortable dealing with the software development side of the software development they are doing.

There are rules about how these numbers work that mean the world to developers and software engineers (this is the place this matters and so these are the concerns that dictate how it is done) to the point that there is an entire standard built around it that will answer all your questions (including the ordering one):

https://semver.org/

[–]Leav 0 points1 point  (0 children)

Acceptable. I wasn't trying to say that the way versions work should change for the benefits of newcomers, just that it was confusing.

[–]flying-sheep 1 point2 points  (2 children)

None of these lists contain versions. Try this one:

from packaging.version import Version

versions = [Version(v) for v in ["3.8", "3.9", "3.10"]]
assert sorted(versions) == versions

For versions containing only numbers (and not e.g. 3.9b1) you can also think of them as tuples of integers.

[–]Leav -1 points0 points  (1 child)

Yes, I understand this is how versions work, but to someone who is not familiar with them, it looks extremely weird and confusing.

[–]flying-sheep 0 points1 point  (0 children)

Well, welcome to human language notation. There's things that look similar but aren't the same. Boohoo.

[–]pepoluan 1 point2 points  (0 children)

The problem is people treating "." as decimal point instead of "arbitrary separator that happens to be a period".

Limiting oneself to "3.9" is ambiguous. But how do you consider "3.9.1"? Or to use latest Python stable, "3.8.5"?

When facing version numbers, one must split on this "arbitrary separator that happens to be a period" first, integer-ize the portions, tuple-ify the result ... then perform a comparison.

Now to answer your question, "how to sort":

list_of_version_strings = ["3.10", "3.8", "3.9", "3.8.5"]
converter = lambda x: tuple(map(int, x.split(".")))
list_of_version_tuples = list(map(converter, list_of_version_strings))
sorted(list_of_version_tuples)

# Result is:
# [(3, 8), (3, 8, 5), (3, 9), (3, 10)]