How to sort a list of time values?sorting based on time a dictionary when the time is h:mm format onlyregex for detecting subtitle errorsHow to merge two dictionaries in a single expression?How do I check if a list is empty?How do I sort a list of dictionaries by a value of the dictionary?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a flat list out of list of lists?Sorting an array of JavaScript objectsSort array of objects by string property valueHow to Sort a List<T> by a property in the objectHow to pair socks from a pile efficiently?
When were female captains banned from Starfleet?
Pre-mixing cryogenic fuels and using only one fuel tank
Shouldn’t conservatives embrace universal basic income?
Doesn't the system of the Supreme Court oppose justice?
Does "he squandered his car on drink" sound natural?
Quoting Keynes in a lecture
A variation to the phrase "hanging over my shoulders"
Why can't the Brexit deadlock in the UK parliament be solved with a plurality vote?
Why is the Sun approximated as a black body at ~ 5800 K?
Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?
How can ping know if my host is down
Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?
Can I turn my anal-retentiveness into a career?
What is the highest possible scrabble score for placing a single tile
Find the next value of this number series
How could a planet have erratic days?
Biological Blimps: Propulsion
What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?
The Digit Triangles
Why do ¬, ∀ and ∃ have the same precedence?
How to convince somebody that he is fit for something else, but not this job?
Why do Radio Buttons not fill the entire outer circle?
Is there any evidence that Cleopatra and Caesarion considered fleeing to India to escape the Romans?
Is this toilet slogan correct usage of the English language?
How to sort a list of time values?
sorting based on time a dictionary when the time is h:mm format onlyregex for detecting subtitle errorsHow to merge two dictionaries in a single expression?How do I check if a list is empty?How do I sort a list of dictionaries by a value of the dictionary?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a flat list out of list of lists?Sorting an array of JavaScript objectsSort array of objects by string property valueHow to Sort a List<T> by a property in the objectHow to pair socks from a pile efficiently?
I have a python list of time values that I extracted from a web log. I have the list in the format of %H:%M:%S. How would I sort the time values in ascending order?
python list sorting time
add a comment |
I have a python list of time values that I extracted from a web log. I have the list in the format of %H:%M:%S. How would I sort the time values in ascending order?
python list sorting time
add a comment |
I have a python list of time values that I extracted from a web log. I have the list in the format of %H:%M:%S. How would I sort the time values in ascending order?
python list sorting time
I have a python list of time values that I extracted from a web log. I have the list in the format of %H:%M:%S. How would I sort the time values in ascending order?
python list sorting time
python list sorting time
edited Mar 8 at 0:51
martineau
69.3k1092186
69.3k1092186
asked Jul 18 '13 at 3:11
intuitionintuition
36115
36115
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
import time
sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)
add a comment |
Just sorted(time_list) works fine.
>>> sorted(["14:10:01", "03:12:08"])
["03:12:08", "14:10:01"]
11
This works because the times are '0' padded.
– SethMMorton
Jul 18 '13 at 3:36
add a comment |
sorted([tuple(map(int, d.split(":"))) for d in my_time_list])
Where each element in my_time_list is of the form you describe, for example:
>>> my_time_list
["03:12:08", "14:10:01"]
it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.
– hago
Jul 18 '13 at 3:32
I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types
– Owen
Jul 18 '13 at 3:42
add a comment |
you should be able use the method sort(key=str.lower) since your time is parsed as a string
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17713873%2fhow-to-sort-a-list-of-time-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
import time
sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)
add a comment |
import time
sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)
add a comment |
import time
sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)
import time
sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)
answered Jul 18 '13 at 3:53
fabrizioMfabrizioM
31k126898
31k126898
add a comment |
add a comment |
Just sorted(time_list) works fine.
>>> sorted(["14:10:01", "03:12:08"])
["03:12:08", "14:10:01"]
11
This works because the times are '0' padded.
– SethMMorton
Jul 18 '13 at 3:36
add a comment |
Just sorted(time_list) works fine.
>>> sorted(["14:10:01", "03:12:08"])
["03:12:08", "14:10:01"]
11
This works because the times are '0' padded.
– SethMMorton
Jul 18 '13 at 3:36
add a comment |
Just sorted(time_list) works fine.
>>> sorted(["14:10:01", "03:12:08"])
["03:12:08", "14:10:01"]
Just sorted(time_list) works fine.
>>> sorted(["14:10:01", "03:12:08"])
["03:12:08", "14:10:01"]
edited Dec 23 '15 at 19:44
AndreL
3,13022034
3,13022034
answered Jul 18 '13 at 3:34
hagohago
1,17821215
1,17821215
11
This works because the times are '0' padded.
– SethMMorton
Jul 18 '13 at 3:36
add a comment |
11
This works because the times are '0' padded.
– SethMMorton
Jul 18 '13 at 3:36
11
11
This works because the times are '0' padded.
– SethMMorton
Jul 18 '13 at 3:36
This works because the times are '0' padded.
– SethMMorton
Jul 18 '13 at 3:36
add a comment |
sorted([tuple(map(int, d.split(":"))) for d in my_time_list])
Where each element in my_time_list is of the form you describe, for example:
>>> my_time_list
["03:12:08", "14:10:01"]
it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.
– hago
Jul 18 '13 at 3:32
I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types
– Owen
Jul 18 '13 at 3:42
add a comment |
sorted([tuple(map(int, d.split(":"))) for d in my_time_list])
Where each element in my_time_list is of the form you describe, for example:
>>> my_time_list
["03:12:08", "14:10:01"]
it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.
– hago
Jul 18 '13 at 3:32
I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types
– Owen
Jul 18 '13 at 3:42
add a comment |
sorted([tuple(map(int, d.split(":"))) for d in my_time_list])
Where each element in my_time_list is of the form you describe, for example:
>>> my_time_list
["03:12:08", "14:10:01"]
sorted([tuple(map(int, d.split(":"))) for d in my_time_list])
Where each element in my_time_list is of the form you describe, for example:
>>> my_time_list
["03:12:08", "14:10:01"]
edited Jul 18 '13 at 3:43
answered Jul 18 '13 at 3:19
OwenOwen
1,500714
1,500714
it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.
– hago
Jul 18 '13 at 3:32
I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types
– Owen
Jul 18 '13 at 3:42
add a comment |
it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.
– hago
Jul 18 '13 at 3:32
I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types
– Owen
Jul 18 '13 at 3:42
it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.
– hago
Jul 18 '13 at 3:32
it returns [[3, 12, 8], [14, 10, 1]]. Data type of elements in returned list is different from the input list, which may not be what he whats.
– hago
Jul 18 '13 at 3:32
I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types
– Owen
Jul 18 '13 at 3:42
I often cast time units to ints because 1) You get type checking and 2) It allows easier interoperability with the datatime module types
– Owen
Jul 18 '13 at 3:42
add a comment |
you should be able use the method sort(key=str.lower) since your time is parsed as a string
add a comment |
you should be able use the method sort(key=str.lower) since your time is parsed as a string
add a comment |
you should be able use the method sort(key=str.lower) since your time is parsed as a string
you should be able use the method sort(key=str.lower) since your time is parsed as a string
answered Jul 18 '13 at 3:25
ConcoConco
363
363
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17713873%2fhow-to-sort-a-list-of-time-values%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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