我有一个字典: dict = [{"DeploymentName": "crisps-case-web-0-v20200304", "ServiceName": "crisps-case-web-0-v20200304", "GrayVersion": "DefaultVersion", "ServiceClusterIp": "10.110.30.244", "ServiceType": "ClusterIP", "CreateTime": "2023-07-07 02:26:34+00:00", "PodIp": "10.100.239.248"}, {"DeploymentName": "crisps-case-web-0-v20200705", "ServiceName": "crisps-case-web-0-v20200705", "GrayVersion": "DefaultVersion", "ServiceClusterIp": "10.110.198.136", "ServiceType": "ClusterIP", "CreateTime": "2023-07-05 08:18:41+00:00", "PodIp": "10.100.239.192"}] 请用CreateTime字段用时间做倒叙重新排列生成
时间: 2023-07-17 13:56:09 浏览: 71
dict_字典文件
4星 · 用户满意度95%
您可以使用Python的`sorted()`函数来对字典列表进行排序,按照CreateTime字段进行倒序排列。以下是一个示例代码:
```python
dict = [
{"DeploymentName": "crisps-case-web-0-v20200304", "ServiceName": "crisps-case-web-0-v20200304", "GrayVersion": "DefaultVersion", "ServiceClusterIp": "10.110.30.244", "ServiceType": "ClusterIP", "CreateTime": "2023-07-07 02:26:34+00:00", "PodIp": "10.100.239.248"},
{"DeploymentName": "crisps-case-web-0-v20200705", "ServiceName": "crisps-case-web-0-v20200705", "GrayVersion": "DefaultVersion", "ServiceClusterIp": "10.110.198.136", "ServiceType": "ClusterIP", "CreateTime": "2023-07-05 08:18:41+00:00", "PodIp": "10.100.239.192"}
]
sorted_dict = sorted(dict, key=lambda x: x['CreateTime'], reverse=True)
```
通过上述代码,您将得到一个新的列表`sorted_dict`,其中的字典按照CreateTime字段进行倒序排列。
希望这能帮到您!如有任何进一步问题,请随时提问。
阅读全文