[{"paymentCreditDate": "2023-05-30","type": "E575","paymentMethod": "PPS Shop & Buy", "totalNum": 1, "totalPaymentAmount": 320 }, { "paymentCreditDate": "2023-05-30", "type": "E575", "paymentMethod": "BOC-FPS", "totalNum": 0, "totalPaymentAmount": 0 }, { "paymentCreditDate": "2023-05-30", "type": "E575", "paymentMethod": "Online Credit Card", "totalNum": 0, "totalPaymentAmount": 0 }, ]将以上数组重新排序为[ { "paymentCreditDate": "2023-05-30", "type": "E575", "paymentMethod": "BOC-FPS", "totalNum": 0, "totalPaymentAmount": 0 }, { "paymentCreditDate": "2023-05-30", "type": "E575", "paymentMethod": "PPS Shop & Buy", "totalNum": 1, "totalPaymentAmount": 320 }, { "paymentCreditDate": "2023-05-30", "type": "E575", "paymentMethod": "Online Credit Card", "totalNum": 0, "totalPaymentAmount": 0 }, ]
时间: 2023-07-05 17:31:28 浏览: 98
日立 HITACHI vm-h675la/VM-E575LA/VM-573LA摄像机维修服务说明书原理图
好的,这是一个数组的操作问题,我们可以通过以下代码来重新排序数组:
```python
arr = [
{
"paymentCreditDate": "2023-05-30",
"type": "E575",
"paymentMethod": "PPS Shop & Buy",
"totalNum": 1,
"totalPaymentAmount": 320
},
{
"paymentCreditDate": "2023-05-30",
"type": "E575",
"paymentMethod": "BOC-FPS",
"totalNum": 0,
"totalPaymentAmount": 0
},
{
"paymentCreditDate": "2023-05-30",
"type": "E575",
"paymentMethod": "Online Credit Card",
"totalNum": 0,
"totalPaymentAmount": 0
},
]
arr.sort(key=lambda x: x['paymentMethod'] == 'BOC-FPS', reverse=True)
```
这样,`arr` 数组就会按照要求的顺序重新排序,结果如下:
```python
[
{
"paymentCreditDate": "2023-05-30",
"type": "E575",
"paymentMethod": "BOC-FPS",
"totalNum": 0,
"totalPaymentAmount": 0
},
{
"paymentCreditDate": "2023-05-30",
"type": "E575",
"paymentMethod": "PPS Shop & Buy",
"totalNum": 1,
"totalPaymentAmount": 320
},
{
"paymentCreditDate": "2023-05-30",
"type": "E575",
"paymentMethod": "Online Credit Card",
"totalNum": 0,
"totalPaymentAmount": 0
},
]
```
代码中的 `key=lambda x: x['paymentMethod'] == 'BOC-FPS'` 表示以 `paymentMethod` 的值是否等于 `'BOC-FPS'` 作为排序依据,而 `reverse=True` 表示按照降序排列。
阅读全文