如何用Python列表推导式移除不含stats键的会话处理者
问题
我有一个存储在变量interactions中的JSON数组,包含呼叫信息、呼叫处理人员,呼叫统计数据存于"stats"键下。部分会话(session)无"stats"键,需移除这些会话对应的处理者(handler),生成新JSON数组。此前用for循环实现但速度慢,请问如何用Python列表推导式完成该操作?
示例输入
[ { "callId": "17f2e9a7-2c82-4908-80f4-019ec2b8be57", "Start": "2023-04-05T16:38:56", "handlers": [ { "handlerId": "dafedee8-2869-414f-970e-657f92016e6b", "Name": "test", "sessions": [ { "ani": "tel:+120134323", "direction": "inbound", "dnis": "tel:+3243423", "requestedRoutings": [ "Standard" ], "sessionId": "c7372835-47f3-461c-b506-e604d0633994", "stats": [ { "name": "talkcnt", "value": 1 }, { "name": "talktime", "value": 903481 } ] } ] }, { "handlerId": "dafeddfds-2869-414f-970e-657f92016e6b", "Name": "test", "sessions": [ { "ani": "tel:+120134323", "direction": "inbound", "dnis": "tel:+3243423", "requestedRoutings": [ "Standard" ], "sessionId": "cdsfd-47f3-461c-b506-e604d0633994", "stats": [ { "name": "talkcnt", "value": 100 }, { "name": "holdtime", "value": 232323 } ] } ] } ] }, { "callId": "23sdf-2c82-4908-80f4-019ec2b8be57", "Start": "2023-04-05T16:38:56", "handlers": [ { "handlerId": "sdfsdfee8-2869-414f-970e-657f92016e6b", "Name": "test", "sessions": [ { "ani": "tel:+120134323", "direction": "inbound", "dnis": "tel:+3243423", "requestedRoutings": [ "Standard" ], "sessionId": "c7372835-47f3-461c-b506-e6sdfd", "stats": [ { "name": "talkcnt", "value": 1 }, { "name": "talktime", "value": 903481 } ] } ] }, { "handlerId": "dafeddfds-2869-414f-970e-657f92016e6b", "Name": "test", "sessions": [ { "ani": "tel:+120134323", "direction": "inbound", "dnis": "tel:+3243423", "requestedRoutings": [ "Standard" ], "sessionId": "abc94" } ] } ] } ]
预期输出
[ { "callId": "17f2e9a7-2c82-4908-80f4-019ec2b8be57", "Start": "2023-04-05T16:38:56", "handlers": [ { "handlerId": "dafedee8-2869-414f-970e-657f92016e6b", "Name": "test", "sessions": [ { "ani": "tel:+120134323", "direction": "inbound", "dnis": "tel:+3243423", "requestedRoutings": [ "Standard" ], "sessionId": "c7372835-47f3-461c-b506-e604d0633994", "stats": [ { "name": "talkcnt", "value": 1 }, { "name": "talktime", "value": 903481 } ] } ] }, { "handlerId": "dafeddfds-2869-414f-970e-657f92016e6b", "Name": "test", "sessions": [ { "ani": "tel:+120134323", "direction": "inbound", "dnis": "tel:+3243423", "requestedRoutings": [ "Standard" ], "sessionId": "cdsfd-47f3-461c-b506-e604d0633994", "stats": [ { "name": "talkcnt", "value": 100 }, { "name": "holdtime", "value": 232323 } ] } ] } ] }, { "callId": "23sdf-2c82-4908-80f4-019ec2b8be57", "Start": "2023-04-05T16:38:56", "handlers": [ { "handlerId": "sdfsdfee8-2869-414f-970e-657f92016e6b", "Name": "test", "sessions": [ { "ani": "tel:+120134323", "direction": "inbound", "dnis": "tel:+3243423", "requestedRoutings": [ "Standard" ], "sessionId": "c7372835-47f3-461c-b506-e6sdfd", "stats": [ { "name": "talkcnt", "value": 1 }, { "name": "talktime", "value": 903481 } ] } ] } ] } ]
解决方案
可以通过嵌套列表推导式实现,利用Python内部对推导式的优化提升执行速度,核心逻辑是筛选出所有会话都包含stats键的处理者:
filtered_interactions = [ { **call, "handlers": [ handler for handler in call["handlers"] if all("stats" in session for session in handler["sessions"]) ] } for call in interactions ]
代码说明
- 外层推导式遍历每个呼叫对象,用
**call保留原呼叫的所有字段,仅重写handlers字段 - 内层推导式筛选处理者:通过
all()函数判断该处理者的所有会话是否都包含stats键,只有满足条件的处理者才会被保留 - 这种写法比普通for循环更简洁,且执行效率更高
内容的提问来源于stack exchange,提问作者chinna7326
相关产品推荐
相关产品推荐

