gpt4 book ai didi

python-3.x - 如何根据特定列对列表字典进行排序?

转载 作者:行者123 更新时间:2023-12-04 12:58:26 24 4
gpt4 key购买 nike

我有一个如下表,存储在字典中:
enter image description here
字典是这样的

d = {
'A': ['45', '70', '5', '88', '93', '79', '87', '69'],
'B': ['99', '18', '91', '3', '92', '2', '67', '15'],
'C': ['199200128', '889172415', '221388292', '199200128', '889172415', '889172415', '199200128', '221388292'],
'D': ['10:27:05', '07:10:29', '17:04:48', '10:25:42', '07:11:18', '07:11:37', '10:38:11', '17:08:55'],
'E': ['73', '6', '95', '21', '29', '15', '99', '9']
}
我想根据从最低到最高的小时数对字典进行排序,并将 A、B 和 E 列与 C 列中与下图所示相同的值相加(其中 A、B 和 E 的总和为红色):
enter image description here
然后,生成的字典将如下所示:
{
'A': ['70', '93', '79', '242', '88', '45', '133', '87', '5', '69', '161'],
'B': ['18', '92', '2', '112', '3', '99', '102', '67', '91', '15', '173'],
'C': ['889172415', '889172415', '889172415', '', '199200128', '199200128', '', '199200128', '221388292', '221388292', ''],
'D': ['07:10:29', '07:11:18', '07:11:37', '', '10:25:42', '10:27:05', '', '10:38:11', '17:04:48', '17:08:55', ''],
'E': ['6', '29', '15', '50', '21', '73', '94', '99', '95', '9', '203']
}
我目前尝试使用此代码对输入字典进行排序,但似乎对我不起作用。
>>> sorted(d.items(), key=lambda e: e[1][4])
[
('D', ['10:27:05', '07:10:29', '17:04:48', '10:25:42', '07:11:18', '07:11:37', '10:38:11', '17:08:55']),
('E', ['73', '6', '95', '21', '29', '15', '99', '9']),
('C', ['199200128', '889172415', '221388292', '199200128', '889172415', '889172415', '199200128', '221388292']),
('B', ['99', '18', '91', '3', '92', '2', '67', '15']),
('A', ['45', '70', '5', '88', '93', '79', '87', '69'])
]
>>>
可能有人对此提供一些帮助。谢谢

最佳答案

你允许使用 Pandas 来解决这个任务吗?
如果是,那么您可以将数据转换为

pd.DataFrame 
目的
data = pd.DataFrame.from_dict(dictionary, orient = 'columns')
data = data.sort_values(by =„D”)
然后使用再次返回字典
_dict = data.to_dict() 

关于python-3.x - 如何根据特定列对列表字典进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62855648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com