以下是一个示例代码,演示如何按照时间戳排序,并在时间戳相同时,按照值进行排序:
data = [
{'timestamp': 1621276678, 'value': 5},
{'timestamp': 1621276681, 'value': 3},
{'timestamp': 1621276678, 'value': 2},
{'timestamp': 1621276680, 'value': 1},
{'timestamp': 1621276679, 'value': 4}
]
# 按照时间戳排序,相同时间戳按照值排序
sorted_data = sorted(data, key=lambda x: (x['timestamp'], x['value']))
# 打印排序结果
for item in sorted_data:
print(item)
运行上述代码,将得到以下输出结果:
{'timestamp': 1621276678, 'value': 2}
{'timestamp': 1621276678, 'value': 5}
{'timestamp': 1621276679, 'value': 4}
{'timestamp': 1621276680, 'value': 1}
{'timestamp': 1621276681, 'value': 3}
根据时间戳进行排序后,相同时间戳的数据按照值进行排序。
上一篇:按照时间戳列的月份进行筛选