我对Python字典有疑问。我将信息存储在字典中。它包含一个时间戳和一个值。我想删除早于5分钟的值。我还想计算字典中其余条目的最大值。我被困在这一点上。有人可以给我一些提示吗?
如何计算字典中的最大值(在这种情况下为4)?
示例词典数据集: [2020-04-27 09:57:57.017258] => 4 [2020-04-27 09:58:29.028821] => 3 [2020-04-27 09:59:00.970389] => 2 [2020-04-27 09:59:32.986955] => 1 [2020-04-27 10:00:05.233491] => 2 [2020-04-27 10:00:38.974868] => 1 [2020-04-27 10:01:14.161092] => 3 [2020-04-27 10:01:47.017566] => 2 [2020-04-27 10:02:18.967178] => 1 [2020-04-27 10:02:51.128727] => 1
提前谢谢! 问候
答案 0 :(得分:1)
如何删除早于datetime.now()。timestamp()-5分钟的键?
keys_to_delete = [k for k in dict if k < datetime.datetime.now() - datetime.timedelta(minutes=5)]
for key in keys_to_delete:
delete dict[key]
如何计算字典中的最大值(在本例中为4)?
max(dict.values())
希望有帮助...