I am trying to write a program that orders a list of strings based on the last character in the item.
["Tiger 6", "Shark 4", "Cyborg 8"]
are how my list is imported, but I need to put them in numerical order based on the numbers at the end.
I am trying to write a program that orders a list of strings based on the last character in the item.
["Tiger 6", "Shark 4", "Cyborg 8"]
are how my list is imported, but I need to put them in numerical order based on the numbers at the end.
Try this if there are more num of digits at the last.
re.search(r'\d+$',x).group()
helps to fetch the number present at the last irrespective of preceding space.If the numbers are not single digits, you can try -
str.rsplit(s, n)
function starts splitting the string at the end towards start of the string and stops after n splits. In the above case, it only splits the string once, at a space.