Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

Version strings to float in python

Quick question. I have a huge csv (100000+ rows) that contains version data (e.g. Python 3.7.3) and I'm trying to compare them to figure out how recent they are. Disregarding regular expressions and extensive list comprehensions, is there a clean and pythonic way to convert strings such as 3.7.3 to a float? For matters of speed and readability I would prefer one or two liners.

Thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Converting a three-dimensional version string to a single float won't give you numbers that you can compare meaningfully in all cases. Consider the versions:

3.15.1
3.7.4
3.7.3
2.20.5

If you simply ignore the second period, you end up with:

3.151
3.74
3.73
2.205

which breaks the ordering.

If you represent these as tuples, e.g.:

(3, 15, 1)
(3, 7, 4)
(3, 7, 3)
(2, 20, 5)

they'll sort correctly, as shown here:

>>> versions = ["3.7.3", "3.15.1", "3.7.4", "2.20.5"]
>>> [tuple(map(int, v.split("."))) for v in versions]
[(3, 7, 3), (3, 15, 1), (3, 7, 4), (2, 20, 5)]
>>> max(tuple(map(int, v.split("."))) for v in versions)
(3, 15, 1)

and if you want to turn it back into a string at the end:

>>> ".".join(map(str, max(tuple(map(int, v.split("."))) for v in versions)))
'3.15.1'

or use the key argument to max to simply do the conversion in-place at the time the comparisons are made:

>>> max(versions, key=lambda v: tuple(map(int, v.split("."))))
'3.15.1'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.7k users

...