Python3,订购一个复杂的字典 [英] Python3, ordering a complex dict
本文介绍了Python3,订购一个复杂的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个复杂的Python字典,该字典存储以下值:MAC地址,RSSI和时间戳:
I've got a complex Python dictionary that stores the following values: MAC ADDRESS, RSSI and TIMESTAMP:
beacons_detected = {
'55:c1:9a:41:4c:b9': ['-78', '1493580469'],
'9c:20:7b:e0:6c:41': ['-74', '1493622425'],
'5e:30:e7:12:97:64': ['-79', '1493587968']
}
我想根据时间戳订购该列表...有关如何实现此目标的任何想法?
I would like to order that list based on timestamp... any idea on how to achieve that?
推荐答案
将字典从最小到最大排序:
sort the dictionary from smallest to largest:
>>> sorted(beacons_detected.items(), key=lambda x: x[1][1])
[('55:c1:9a:41:4c:b9', ['-78', '1493580469']), ('5e:30:e7:12:97:64', ['-79', '1493587968']), ('9c:20:7b:e0:6c:41', ['-74', '1493622425'])]
将字典从大到小排序:
>>> sorted(beacons_detected.items(), key=lambda x: x[1][1], reverse=True)
[('9c:20:7b:e0:6c:41', ['-74', '1493622425']), ('5e:30:e7:12:97:64', ['-79', '1493587968']), ('55:c1:9a:41:4c:b9', ['-78', '1493580469'])]
这篇关于Python3,订购一个复杂的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文