Python3,订购复杂的字典

标签 python dictionary

我有一个复杂的 Python 字典,它存储以下值: MAC 地址、RSSI 和时间戳:

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']
}

我想根据时间戳对该列表进行排序...知道如何实现吗?

最佳答案

将字典从小到大排序:

>>> 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,订购复杂的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43715921/

相关文章:

Python:填充多维数组

python - dis 模块中的 `LOAD_FAST`

python - 如何使用 setup.py 安装托管在私有(private) PyPI 中的包?

java - 如何在 JMapViewer 中表示路由

python - 如何正确排序 Python 字典中的项目?

java - dom4j.Node 迭代元素映射

python - 当 x = y 时,Numpy 和 R 在线性回归中给出非零截距

python - PySide/PyQt 如何创建一个新的窗口按钮?

c++ - std::pair 和类析构函数

python - 将新的字典值列添加到 pandas 数据框中