python - 删除带有条件的列表中的字典
问题描述
我有下面的字典列表,我需要删除具有相同received_on
和customer_group
值的字典,但留下一个随机的项目。
data = [
{
'id': '16e26a4a9f97fa4f',
'received_on': '2019-11-01 11:05:51',
'customer_group': 'Life-time Buyer'
},
{
'id': '16db0dd4a42673e2',
'received_on': '2019-10-09 14:12:29',
'customer_group': 'Lead'
},
{
'id': '16db0dd4199f5897',
'received_on': '2019-10-09 14:12:29',
'customer_group': 'Lead'
}
]
预期输出:
[
{
'id': '16e26a4a9f97fa4f',
'received_on': '2019-11-01 11:05:51',
'customer_group': 'Life-time Buyer'
},
{
'id': '16db0dd4199f5897',
'received_on': '2019-10-09 14:12:29',
'customer_group': 'Lead'
}
]
解决方案
使用上面的一些想法,我还想customer_group
将received_on
. 我得到了我的预期结果。
conditions, result = [], []
for d in data:
condition = (d['received_on'], d['customer_group'])
if condition not in conditions:
result.append(d)
conditions.append(condition)
print(len(result))
推荐阅读
- ami.js - 提供平滑的阴影材料
- php - Yii 1.2 框架命令行迁移文件不运行?
- xml - XML 的符号列表定义
- python - Pandas:读/写具有不同行分隔符的 CSV 文件
- powerbi - PowerBi - 我们可以嵌入软件并分发给客户吗?
- gradle - 尝试将 Java 项目从 GIT 克隆到 intellij 时,无法找到请求目标的有效认证路径
- angular - 如何在 Angular 4 服务中使用包含“/”的字符串参数发送获取请求?
- android - 在奥利奥中显示粉红色的导航抽屉
- javascript - 通过 Grafana 数据源插件的路由进行数据源身份验证
- angular - 角度 2 4 5 - 无法读取未定义角度 5 的属性“名称”