Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorted() with lambda function

I have strings that look like "co1/co2", "co3/co4" ... "co11/co12"

describing it as a regex:

^(?P<prefix>\w\w)(?P<month>\d+)/(?P<prefix2>\w\w)(?P<month2>\d+)$   

I want to sort a collection of these strings based on the equivalent of the "month" group of the regex. (the first number in the string (eg. the "1" in 'co1/co2' or the "12" in 'co12/co13')

I'm unable to figure out a lambda function I can use in sorted() that will do this for me, though.

wrong_order = [u'co1/co2', u'co10/co11', u'co11/co12', u'co12/co13', u'co2/co3',
       u'co3/co4', u'co4/co5', u'co5/co6', u'co6/co7', u'co7/co8', u'co8/co9',
       u'co9/co10']


correct_order = [u'co1/co2', u'co2/co3', u'co3/co4', u'co4/co5', u'co5/co6', \
u'co6/co7', u'co7/co8', u'co8/co9', u'co9/co10', u'co10/co11', u'co11/co12', u'co12/co13']


#this lambda function doesn't work
output = sorted(wrong_order, key=lambda x: (x[2:]))
like image 735
user3556757 Avatar asked Feb 09 '23 19:02

user3556757


2 Answers

Without a regex:

lambda x: int(x.partition('/')[0][2:])

This takes the part of the string before the /, then everything but the starting co, then converts that to an integer.

I used str.partition() as it is faster than str.split() for the split only once case.

Demo:

>>> wrong_order = [u'co1/co2', u'co10/co11', u'co11/co12', u'co12/co13', u'co2/co3',
...        u'co3/co4', u'co4/co5', u'co5/co6', u'co6/co7', u'co7/co8', u'co8/co9',
...        u'co9/co10']
>>> sorted(wrong_order, key=lambda x: int(x.partition('/')[0][2:]))
[u'co1/co2', u'co2/co3', u'co3/co4', u'co4/co5', u'co5/co6', u'co6/co7', u'co7/co8', u'co8/co9', u'co9/co10', u'co10/co11', u'co11/co12', u'co12/co13']
like image 82
Martijn Pieters Avatar answered Feb 15 '23 11:02

Martijn Pieters


try this:

>>> sorted(wrong_order, key = lambda x:int(x.split("/")[0][2:]))
[u'co1/co2', u'co2/co3', u'co3/co4', u'co4/co5', u'co5/co6', u'co6/co7', u'co7/co8', u'co8/co9', u'co9/co10', u'co10/co11', u'co11/co12', u'co12/co13']

Lambda whats it doing, first it split with "/" and select the 1st value and select the value after 1 index and convert to integer

like image 44
Hackaholic Avatar answered Feb 15 '23 11:02

Hackaholic