I am trying to get the highest version of a string in Python. I was trying to sort the list but that of course doesnt work as easily as Python will sort the string representation.
For that I am trying to work with regex but it somehow doesnt match.
The Strings look like this:
topic_v10_ext2
topic_v20_ext2
topic_v2_ext2
topic_v5_ext2
topic_v7_ext2
My Regex looks like this.
version_no = re.search("(?:_v([0-9]+))?", v.name)
I was thinking about saving the names in a list and look for the highest v_xx in the list to return. Also for now I am doing this in two FOR loops. Which runs in 2*O(log(n)) which is not optimal I believe. How can I get the highest version in a fast and simple way?
It provides a __version__ attribute. It provides the standard metadata version. Therefore it will be detected by pkg_resources or other tools that parse the package metadata (EGG-INFO and/or PKG-INFO, PEP 0345).
You can check the version of Python that is running a program, at runtime. Then check the content of the sys. version_info property. This property returns the Python version as a tuple.
Python program to find largest number in a list. Given a list of numbers, the task is to write a Python program to find the largest number in given list. Examples: Input : list1 = [10, 20, 4] Output : 20 Input : list2 = [20, 10, 20, 4, 100] Output : 100. Method 1 : Sort the list in ascending order and print the last element in the list.
PythonProgrammingServer Side Programming. The easiest way to extract the maximum numeric value from a string using regex is to −. Use the regex module to extract all the numbers from a string. Find the max from these numbers.
Compare Version Numbers in Python 1 version1_arr = an array of numbers separated by dot for version1 2 version2_arr = an array of numbers separated by dot for version2 3 for i in range 0 to max of size of version1_arr and size of version2_arr − v1 := version1_arr [i] if i < size of version1_arr, otherwise 0 v2 := ... 4 return 0
To get the latest version you could just select the last element in the list versions[-1]or reverse sort by using the reverseattribute of sorted(), setting it to True, and getting the [0]element. You could of course then wrap all this up in a convenient function for reuse.
You can use sorted
or list.sort
with key
:
sorted(l, key=lambda x:int(x.split('_')[1][1:]), reverse=True)
['topic_v20_ext2',
'topic_v10_ext2',
'topic_v7_ext2',
'topic_v5_ext2',
'topic_v2_ext2']
x.split('_')
: returns splitted str
, e.g.: ['topic', 'v20', 'ext2']
x.split('_')[1]
V20
has unwanted character 'V', thus reselect it by slicing [1:]
to get all the digits.int
for numerical ordering.Also, sorted
by default returns ascending order of sort. Since you require descending order, use reverse=True
.
It could also work with regular expressions, as first tried:
import re
v = 'topic_v7_ext2'
version_no = re.search("^[^_]*_v([0-9]+)", v)
print(version_no.group(1))
That expression searches for pattern from the beginning of the string (^), takes all characters different from _ (I hope your topics can't have one, else both answers are wrong), then finds the '_v' and takes the version number.
There is no need to match _ext, so it doesn't matter if it's there or not!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With