Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the highest String Version number in Python

Tags:

python

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?

like image 967
Derek Haynes Avatar asked Feb 22 '19 14:02

Derek Haynes


People also ask

What is __ version __ in Python?

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).

What version of Python do I have Python?

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.

How to find largest number in a list in Python?

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.

How do I get the maximum value from a string Python?

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.

How to compare version numbers in Python?

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

How to get the latest version of a list in Python?

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.


2 Answers

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']
  • Since the version is the key to the sorting, select it by x.split('_')[1]
  • Selected V20 has unwanted character 'V', thus reselect it by slicing [1:] to get all the digits.
  • Finally, convert digits to int for numerical ordering.

Also, sorted by default returns ascending order of sort. Since you require descending order, use reverse=True.

like image 78
Chris Avatar answered Nov 02 '22 10:11

Chris


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!

like image 27
B. Go Avatar answered Nov 02 '22 10:11

B. Go