在Python中按日期时间对字典按值进行排序

huangapple go评论65阅读模式
英文:

Sorting dictionary by value by datetime in python

问题

如何按字典中值的日期时间值对其进行排序,形式如下:

{'user_0': [['item_805696', '2021-02-11 13:03:42'],
  ['item_386903', '2021-02-11 13:03:52'],
  ['item_3832', '2021-02-11 13:04:07'],
  ['item_849824', '2021-02-11 13:05:04']],
 'user_1': [['item_97057', '2021-02-11 13:03:42'],
  ['item_644971', '2021-02-11 13:09:32'],
  ['item_947129', '2021-02-11 13:15:27'],
  ['item_58840', '2021-02-11 13:16:11'],
  ['item_640213', '2021-02-11 13:17:40'],
  ...
}

我尝试按字典值中第二个值的日期时间进行排序。

英文:

How do I sort a dictionary by value in datetime in shape of this:

{'user_0': [['item_805696', '2021-02-11 13:03:42'],
  ['item_386903', '2021-02-11 13:03:52'],
  ['item_3832', '2021-02-11 13:04:07'],
  ['item_849824', '2021-02-11 13:05:04'],
'user_1': [['item_97057', '2021-02-11 13:03:42'],
  ['item_644971', '2021-02-11 13:09:32'],
  ['item_947129', '2021-02-11 13:15:27'],
  ['item_58840', '2021-02-11 13:16:11'],
  ['item_640213', '2021-02-11 13:17:40'],
...

Im trying to sort values by datetime of second value in values of the dictionary

答案1

得分: 0

你可以按照此答案的方式将表示日期的字符串转换为自Unix纪元(1970年1月1日)以来的秒数:https://stackoverflow.com/questions/30468371/how-to-convert-python-timestamp-string-to-epoch

然后可以使用sorted内置方法的key参数来排序字典以满足你的需求。

英文:

You can follow this answer to convert the string representing the date to a number of seconds since the Unix epoch (1st January 1970): https://stackoverflow.com/questions/30468371/how-to-convert-python-timestamp-string-to-epoch

Then use can use the key argument of the sorted built-in method to use it to sort the dictionary how you want.

答案2

得分: 0

你可以给list.sortsorted提供一个key来指定排序方式。

按第一个值排序的keylambda x: x[0];按第二个值排序的keylambda x: x[1]

d = {'user_0': [['item_805696', '2021-02-11 13:03:42'],
                ['item_849824', '2021-02-11 13:05:04'],
                ['item_386903', '2021-02-11 13:03:52'],
                ['item_3832',   '2021-02-11 13:04:07']],
     'user_1': [['item_58840',  '2021-02-11 13:16:11'],
                ['item_947129', '2021-02-11 13:15:27'],
                ['item_97057',  '2021-02-11 13:03:42'],
                ['item_640213', '2021-02-11 13:17:40'],
                ['item_644971', '2021-02-11 13:09:32']]}

for v in d.values():
    v.sort(key=lambda x: x[1])

print(d)
# {'user_0': [['item_805696', '2021-02-11 13:03:42'],
#             ['item_386903', '2021-02-11 13:03:52'],
#             ['item_3832',   '2021-02-11 13:04:07'],
#             ['item_849824', '2021-02-11 13:05:04']],
#  'user_1': [['item_97057',  '2021-02-11 13:03:42'],
#             ['item_644971', '2021-02-11 13:09:32'],
#             ['item_947129', '2021-02-11 13:15:27'],
#             ['item_58840',  '2021-02-11 13:16:11'],
#             ['item_640213', '2021-02-11 13:17:40']]}

请注意,这仅适用于时间戳以非常实用的格式'yyyy-mm-dd HH:MM:SS'呈现,并且字符串是按字典顺序排序的,即从左到右排序。如果时间戳采用不太友好的格式,比如'dd-mm-yyyy HH:MM:SS',那么您需要使用key来解析时间戳。

英文:

You can give a key to tell list.sort or sorted how to sort.

A key to sort by the first value is lambda x: x[0]; a key to sort by the second value is lambda x: x[1].

d = {'user_0': [['item_805696', '2021-02-11 13:03:42'],
                ['item_849824', '2021-02-11 13:05:04'],
                ['item_386903', '2021-02-11 13:03:52'],
                ['item_3832',   '2021-02-11 13:04:07']],
     'user_1': [['item_58840',  '2021-02-11 13:16:11'],
                ['item_947129', '2021-02-11 13:15:27'],
                ['item_97057',  '2021-02-11 13:03:42'],
                ['item_640213', '2021-02-11 13:17:40'],
                ['item_644971', '2021-02-11 13:09:32']]}

for v in d.values():
    v.sort(key=lambda x: x[1])

print(d)
# {'user_0': [['item_805696', '2021-02-11 13:03:42'],
#             ['item_386903', '2021-02-11 13:03:52'],
#             ['item_3832',   '2021-02-11 13:04:07'],
#             ['item_849824', '2021-02-11 13:05:04']],
#  'user_1': [['item_97057',  '2021-02-11 13:03:42'],
#             ['item_644971', '2021-02-11 13:09:32'],
#             ['item_947129', '2021-02-11 13:15:27'],
#             ['item_58840',  '2021-02-11 13:16:11'],
#             ['item_640213', '2021-02-11 13:17:40']]}

Note that this only works because the timestamps are presented in the very practical format 'yyyy-mm-dd HH:MM:SS', and strings are sorted lexicographically, i.e., from left to right. If the timestamps had been in a less friendly format, such as 'dd-mm-yyyy HH:MM:SS', then you'd need to use the key to parse the timestamps.

答案3

得分: 0

你也可以在Python标准库中使用datetime模块,以下是代码部分:

from datetime import datetime

d = {'user_0': [['item_805696', '2021-02-11 13:03:42'],
  ['item_386903', '2021-02-11 13:03:52'],
  ['item_3832', '2021-02-11 13:04:07'],
  ['item_849824', '2021-02-11 13:05:04']],
 'user_1': [['item_97057', '2021-02-11 13:03:42'],
  ['item_644971', '2021-02-11 13:09:32'],
  ['item_947129', '2021-02-11 13:15:27'],
  ['item_58840', '2021-02-11 13:16:11'],
  ['item_640213', '2021-02-11 13:17:40']]}

def sort_by_datetime(val):
    return datetime.strptime(val[1], '%Y-%m-%d %H:%M:%S')

sorted_dict = {k: sorted(v, key=sort_by_datetime) for k, v in d.items()}
英文:

You can also use the datetime module in python standard librairy and here is the code

from datetime import datetime

d = {'user_0': [['item_805696', '2021-02-11 13:03:42'],
  ['item_386903', '2021-02-11 13:03:52'],
  ['item_3832', '2021-02-11 13:04:07'],
  ['item_849824', '2021-02-11 13:05:04']],
'user_1': [['item_97057', '2021-02-11 13:03:42'],
  ['item_644971', '2021-02-11 13:09:32'],
  ['item_947129', '2021-02-11 13:15:27'],
  ['item_58840', '2021-02-11 13:16:11'],
  ['item_640213', '2021-02-11 13:17:40']]}

def sort_by_datetime(val):
    return datetime.strptime(val[1], '%Y-%m-%d %H:%M:%S')

sorted_dict = {k: sorted(v, key=sort_by_datetime) for k, v in d.items()}

huangapple
  • 本文由 发表于 2023年2月6日 04:29:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定