How do I sort this list via the numerical values? Is a regex required to remove the numbers or is there a more Pythonic way to do this?
to_sort
['12-foo',
'1-bar',
'2-bar',
'foo-11',
'bar-3',
'foo-4',
'foobar-5',
'6-foo',
'7-bar']
Desired output is as follows:
1-bar
2-bar
bar-3
foo-4
foobar-5
6-foo
7-bar
foo-11
12-foo
Definition and Usage Note: You cannot sort a list that contains BOTH string values AND numeric values.
Method #2 : Using sorted() + key + lambda + isdigit() The combination of above functionalities can also be used to achieve solution to this problem. In this, we just sort the list using sorted() using key functionality using lambda function to segregate digits using isdigit().
The sort() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.
Sort String in Java String in Java is immutable. There is no direct method to sort a string in Java. You can use Arrays, which has a method CharArray() that will create a char input string and using another method (Arrays.
One solution is the following regex extraction:
sorted(l, key=lambda x: int(re.search('\d+', x).group(0)))
>>> l
['12-foo', '1-bar', '2-bar', 'foo-11', 'bar-3', 'foo-4', 'foobar-5', '6-foo', '7-bar']
>>> sorted(l, key=lambda x: int(re.search('\d+', x).group(0)))
['1-bar', '2-bar', 'bar-3', 'foo-4', 'foobar-5', '6-foo', '7-bar', 'foo-11', '12-foo']
The key
is the extracted digit (converted to int
to avoid sorting lexographically).
If you don't want to use regex
>>> l = ['12-foo', '1-bar', '2-bar', 'foo-11', 'bar-3', 'foo-4', 'foobar-5', '6-foo', '7-bar']
>>> sorted(l, key = lambda x: int(''.join(filter(str.isdigit, x))))
['1-bar', '2-bar', 'bar-3', 'foo-4', 'foobar-5', '6-foo', '7-bar', 'foo-11', '12-foo']
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