sort a list using lambda and regex in python










1















list = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime', 'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 'yyyyy' ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime]

datet = re.compile(r'ResultDatetime:(d4-d2-d2 d2:d2)')

list.sort(key = lambda x: ........)


I want to sort the lists in an order starting with the earliest date. How should I go about it using lambda and regex?










share|improve this question
























  • Why do you have these weird strings? What is the expected output for the given list?

    – timgeb
    Nov 14 '18 at 16:33











  • sorry the original string had '<' characters in it which interfered with the way it was displayed. I have edited the question as you can see now

    – dratoms
    Nov 14 '18 at 16:34






  • 1





    Avoid list as a variable name, there's already the builtin list.

    – timgeb
    Nov 14 '18 at 16:51











  • yes list should not have been used as a variable name. thnx

    – dratoms
    Nov 14 '18 at 17:07















1















list = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime', 'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 'yyyyy' ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime]

datet = re.compile(r'ResultDatetime:(d4-d2-d2 d2:d2)')

list.sort(key = lambda x: ........)


I want to sort the lists in an order starting with the earliest date. How should I go about it using lambda and regex?










share|improve this question
























  • Why do you have these weird strings? What is the expected output for the given list?

    – timgeb
    Nov 14 '18 at 16:33











  • sorry the original string had '<' characters in it which interfered with the way it was displayed. I have edited the question as you can see now

    – dratoms
    Nov 14 '18 at 16:34






  • 1





    Avoid list as a variable name, there's already the builtin list.

    – timgeb
    Nov 14 '18 at 16:51











  • yes list should not have been used as a variable name. thnx

    – dratoms
    Nov 14 '18 at 17:07













1












1








1








list = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime', 'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 'yyyyy' ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime]

datet = re.compile(r'ResultDatetime:(d4-d2-d2 d2:d2)')

list.sort(key = lambda x: ........)


I want to sort the lists in an order starting with the earliest date. How should I go about it using lambda and regex?










share|improve this question
















list = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime', 'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 'yyyyy' ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime]

datet = re.compile(r'ResultDatetime:(d4-d2-d2 d2:d2)')

list.sort(key = lambda x: ........)


I want to sort the lists in an order starting with the earliest date. How should I go about it using lambda and regex?







python regex lambda






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 16:59









timgeb

50.9k116593




50.9k116593










asked Nov 14 '18 at 16:31









dratomsdratoms

195




195












  • Why do you have these weird strings? What is the expected output for the given list?

    – timgeb
    Nov 14 '18 at 16:33











  • sorry the original string had '<' characters in it which interfered with the way it was displayed. I have edited the question as you can see now

    – dratoms
    Nov 14 '18 at 16:34






  • 1





    Avoid list as a variable name, there's already the builtin list.

    – timgeb
    Nov 14 '18 at 16:51











  • yes list should not have been used as a variable name. thnx

    – dratoms
    Nov 14 '18 at 17:07

















  • Why do you have these weird strings? What is the expected output for the given list?

    – timgeb
    Nov 14 '18 at 16:33











  • sorry the original string had '<' characters in it which interfered with the way it was displayed. I have edited the question as you can see now

    – dratoms
    Nov 14 '18 at 16:34






  • 1





    Avoid list as a variable name, there's already the builtin list.

    – timgeb
    Nov 14 '18 at 16:51











  • yes list should not have been used as a variable name. thnx

    – dratoms
    Nov 14 '18 at 17:07
















Why do you have these weird strings? What is the expected output for the given list?

– timgeb
Nov 14 '18 at 16:33





Why do you have these weird strings? What is the expected output for the given list?

– timgeb
Nov 14 '18 at 16:33













sorry the original string had '<' characters in it which interfered with the way it was displayed. I have edited the question as you can see now

– dratoms
Nov 14 '18 at 16:34





sorry the original string had '<' characters in it which interfered with the way it was displayed. I have edited the question as you can see now

– dratoms
Nov 14 '18 at 16:34




1




1





Avoid list as a variable name, there's already the builtin list.

– timgeb
Nov 14 '18 at 16:51





Avoid list as a variable name, there's already the builtin list.

– timgeb
Nov 14 '18 at 16:51













yes list should not have been used as a variable name. thnx

– dratoms
Nov 14 '18 at 17:07





yes list should not have been used as a variable name. thnx

– dratoms
Nov 14 '18 at 17:07












3 Answers
3






active

oldest

votes


















1














With the code you have there it is sufficient to do:



list.sort(key=lambda x: datet.search(x).group(1))


(but please, don't use list as a variable name).



There is no need to convert the extracted string to a datetime as it is already in a format that will sort naturally.



Note however that if any string does not match the regex this will generate an error, so you may be better to split the key out into a named multi-line function and test for a successful match before returning the matched group.



def sort_key(line): 
match = datet.search(line)
if match:
return match.group(1)
return ''

data = [
'xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime'
]
data.sort(key=sort_key)





share|improve this answer























  • Ah, good observation about the lexicographical order.

    – timgeb
    Nov 14 '18 at 16:58











  • thanks for that syntax. that was elusive to me. and thanks for that neat little function there. Although the list element part is autogenerated and is unlikely that there will be missing values, your function is going to help me a lot in future, a newbie to python (and programming in general) that I am.

    – dratoms
    Nov 14 '18 at 17:13


















0














You can use dateutil.parser.parse (see this answer: Parse date strings?) to parse the date and re.findall to get it from a string



import re 
from dateutil.parser import parse

list = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime', 'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime]
datet = re.compile(r'ResultDatetime:(d4-d2-d2 d2:d2)')

list.sort(key = lambda x : parse(re.findall(datet, x)[0]))





share|improve this answer























  • I haven't used dateutil so far. But it seems promising. Will keep this in mind.

    – dratoms
    Nov 14 '18 at 17:15


















0














I think the simplest solution without any imports would be:



data = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime']

sorted_data = sorted(data, key=lambda x: x[20:36])

print(sorted_data)


Output:



 ['xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 
'xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime']





share|improve this answer























  • The last string has the date at a slightly different offset. I think the OP's intention is that xxxx and yyyyy could be any arbitrarily long strings.

    – Duncan
    Nov 14 '18 at 17:08











  • exactly. and there could be other string numbers before the regex pattern that would impede in natural sorting here.

    – dratoms
    Nov 14 '18 at 17:20










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304794%2fsort-a-list-using-lambda-and-regex-in-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














With the code you have there it is sufficient to do:



list.sort(key=lambda x: datet.search(x).group(1))


(but please, don't use list as a variable name).



There is no need to convert the extracted string to a datetime as it is already in a format that will sort naturally.



Note however that if any string does not match the regex this will generate an error, so you may be better to split the key out into a named multi-line function and test for a successful match before returning the matched group.



def sort_key(line): 
match = datet.search(line)
if match:
return match.group(1)
return ''

data = [
'xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime'
]
data.sort(key=sort_key)





share|improve this answer























  • Ah, good observation about the lexicographical order.

    – timgeb
    Nov 14 '18 at 16:58











  • thanks for that syntax. that was elusive to me. and thanks for that neat little function there. Although the list element part is autogenerated and is unlikely that there will be missing values, your function is going to help me a lot in future, a newbie to python (and programming in general) that I am.

    – dratoms
    Nov 14 '18 at 17:13















1














With the code you have there it is sufficient to do:



list.sort(key=lambda x: datet.search(x).group(1))


(but please, don't use list as a variable name).



There is no need to convert the extracted string to a datetime as it is already in a format that will sort naturally.



Note however that if any string does not match the regex this will generate an error, so you may be better to split the key out into a named multi-line function and test for a successful match before returning the matched group.



def sort_key(line): 
match = datet.search(line)
if match:
return match.group(1)
return ''

data = [
'xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime'
]
data.sort(key=sort_key)





share|improve this answer























  • Ah, good observation about the lexicographical order.

    – timgeb
    Nov 14 '18 at 16:58











  • thanks for that syntax. that was elusive to me. and thanks for that neat little function there. Although the list element part is autogenerated and is unlikely that there will be missing values, your function is going to help me a lot in future, a newbie to python (and programming in general) that I am.

    – dratoms
    Nov 14 '18 at 17:13













1












1








1







With the code you have there it is sufficient to do:



list.sort(key=lambda x: datet.search(x).group(1))


(but please, don't use list as a variable name).



There is no need to convert the extracted string to a datetime as it is already in a format that will sort naturally.



Note however that if any string does not match the regex this will generate an error, so you may be better to split the key out into a named multi-line function and test for a successful match before returning the matched group.



def sort_key(line): 
match = datet.search(line)
if match:
return match.group(1)
return ''

data = [
'xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime'
]
data.sort(key=sort_key)





share|improve this answer













With the code you have there it is sufficient to do:



list.sort(key=lambda x: datet.search(x).group(1))


(but please, don't use list as a variable name).



There is no need to convert the extracted string to a datetime as it is already in a format that will sort naturally.



Note however that if any string does not match the regex this will generate an error, so you may be better to split the key out into a named multi-line function and test for a successful match before returning the matched group.



def sort_key(line): 
match = datet.search(line)
if match:
return match.group(1)
return ''

data = [
'xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime'
]
data.sort(key=sort_key)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 16:54









DuncanDuncan

63k679132




63k679132












  • Ah, good observation about the lexicographical order.

    – timgeb
    Nov 14 '18 at 16:58











  • thanks for that syntax. that was elusive to me. and thanks for that neat little function there. Although the list element part is autogenerated and is unlikely that there will be missing values, your function is going to help me a lot in future, a newbie to python (and programming in general) that I am.

    – dratoms
    Nov 14 '18 at 17:13

















  • Ah, good observation about the lexicographical order.

    – timgeb
    Nov 14 '18 at 16:58











  • thanks for that syntax. that was elusive to me. and thanks for that neat little function there. Although the list element part is autogenerated and is unlikely that there will be missing values, your function is going to help me a lot in future, a newbie to python (and programming in general) that I am.

    – dratoms
    Nov 14 '18 at 17:13
















Ah, good observation about the lexicographical order.

– timgeb
Nov 14 '18 at 16:58





Ah, good observation about the lexicographical order.

– timgeb
Nov 14 '18 at 16:58













thanks for that syntax. that was elusive to me. and thanks for that neat little function there. Although the list element part is autogenerated and is unlikely that there will be missing values, your function is going to help me a lot in future, a newbie to python (and programming in general) that I am.

– dratoms
Nov 14 '18 at 17:13





thanks for that syntax. that was elusive to me. and thanks for that neat little function there. Although the list element part is autogenerated and is unlikely that there will be missing values, your function is going to help me a lot in future, a newbie to python (and programming in general) that I am.

– dratoms
Nov 14 '18 at 17:13













0














You can use dateutil.parser.parse (see this answer: Parse date strings?) to parse the date and re.findall to get it from a string



import re 
from dateutil.parser import parse

list = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime', 'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime]
datet = re.compile(r'ResultDatetime:(d4-d2-d2 d2:d2)')

list.sort(key = lambda x : parse(re.findall(datet, x)[0]))





share|improve this answer























  • I haven't used dateutil so far. But it seems promising. Will keep this in mind.

    – dratoms
    Nov 14 '18 at 17:15















0














You can use dateutil.parser.parse (see this answer: Parse date strings?) to parse the date and re.findall to get it from a string



import re 
from dateutil.parser import parse

list = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime', 'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime]
datet = re.compile(r'ResultDatetime:(d4-d2-d2 d2:d2)')

list.sort(key = lambda x : parse(re.findall(datet, x)[0]))





share|improve this answer























  • I haven't used dateutil so far. But it seems promising. Will keep this in mind.

    – dratoms
    Nov 14 '18 at 17:15













0












0








0







You can use dateutil.parser.parse (see this answer: Parse date strings?) to parse the date and re.findall to get it from a string



import re 
from dateutil.parser import parse

list = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime', 'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime]
datet = re.compile(r'ResultDatetime:(d4-d2-d2 d2:d2)')

list.sort(key = lambda x : parse(re.findall(datet, x)[0]))





share|improve this answer













You can use dateutil.parser.parse (see this answer: Parse date strings?) to parse the date and re.findall to get it from a string



import re 
from dateutil.parser import parse

list = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime', 'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime]
datet = re.compile(r'ResultDatetime:(d4-d2-d2 d2:d2)')

list.sort(key = lambda x : parse(re.findall(datet, x)[0]))






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 16:58









mrzasamrzasa

10.6k104078




10.6k104078












  • I haven't used dateutil so far. But it seems promising. Will keep this in mind.

    – dratoms
    Nov 14 '18 at 17:15

















  • I haven't used dateutil so far. But it seems promising. Will keep this in mind.

    – dratoms
    Nov 14 '18 at 17:15
















I haven't used dateutil so far. But it seems promising. Will keep this in mind.

– dratoms
Nov 14 '18 at 17:15





I haven't used dateutil so far. But it seems promising. Will keep this in mind.

– dratoms
Nov 14 '18 at 17:15











0














I think the simplest solution without any imports would be:



data = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime']

sorted_data = sorted(data, key=lambda x: x[20:36])

print(sorted_data)


Output:



 ['xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 
'xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime']





share|improve this answer























  • The last string has the date at a slightly different offset. I think the OP's intention is that xxxx and yyyyy could be any arbitrarily long strings.

    – Duncan
    Nov 14 '18 at 17:08











  • exactly. and there could be other string numbers before the regex pattern that would impede in natural sorting here.

    – dratoms
    Nov 14 '18 at 17:20















0














I think the simplest solution without any imports would be:



data = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime']

sorted_data = sorted(data, key=lambda x: x[20:36])

print(sorted_data)


Output:



 ['xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 
'xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime']





share|improve this answer























  • The last string has the date at a slightly different offset. I think the OP's intention is that xxxx and yyyyy could be any arbitrarily long strings.

    – Duncan
    Nov 14 '18 at 17:08











  • exactly. and there could be other string numbers before the regex pattern that would impede in natural sorting here.

    – dratoms
    Nov 14 '18 at 17:20













0












0








0







I think the simplest solution without any imports would be:



data = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime']

sorted_data = sorted(data, key=lambda x: x[20:36])

print(sorted_data)


Output:



 ['xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 
'xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime']





share|improve this answer













I think the simplest solution without any imports would be:



data = ['xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime']

sorted_data = sorted(data, key=lambda x: x[20:36])

print(sorted_data)


Output:



 ['xxxx ResultDatetime:2017-05-26 15:36:00.000:ResultDatetime', 
'xxxx ResultDatetime:2017-05-31 09:38:00.000:ResultDatetime',
'yyyyy ResultDatetime:2017-10-23 16:16:00.000:ResultDatetime']






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 17:06









NickNick

79911231




79911231












  • The last string has the date at a slightly different offset. I think the OP's intention is that xxxx and yyyyy could be any arbitrarily long strings.

    – Duncan
    Nov 14 '18 at 17:08











  • exactly. and there could be other string numbers before the regex pattern that would impede in natural sorting here.

    – dratoms
    Nov 14 '18 at 17:20

















  • The last string has the date at a slightly different offset. I think the OP's intention is that xxxx and yyyyy could be any arbitrarily long strings.

    – Duncan
    Nov 14 '18 at 17:08











  • exactly. and there could be other string numbers before the regex pattern that would impede in natural sorting here.

    – dratoms
    Nov 14 '18 at 17:20
















The last string has the date at a slightly different offset. I think the OP's intention is that xxxx and yyyyy could be any arbitrarily long strings.

– Duncan
Nov 14 '18 at 17:08





The last string has the date at a slightly different offset. I think the OP's intention is that xxxx and yyyyy could be any arbitrarily long strings.

– Duncan
Nov 14 '18 at 17:08













exactly. and there could be other string numbers before the regex pattern that would impede in natural sorting here.

– dratoms
Nov 14 '18 at 17:20





exactly. and there could be other string numbers before the regex pattern that would impede in natural sorting here.

– dratoms
Nov 14 '18 at 17:20

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304794%2fsort-a-list-using-lambda-and-regex-in-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto