python 列表套json字典根据相同的key筛选数据
作者:shine?stone 时间:2022-06-01 08:52:02
前言:
工作中遇到以下小问题,解决方法如下,可能比较暴力,暂时留档,再进行优化。
要求:将列表中json的 ‘id’ 字段值相同的数据,根据 type的值,按照一定的优先级次序排列,列表中仅保留优先级最高的type
。
测试用例:
list1 示例数据:
type优先级列表:[6, 4, 2, 5, 8, 3, 7, 1] (依次递减,6优先级最高,1优先级最低)
draw_data = [
{'geometry':{"coordinates":[121.87635833333333, 30.86567777777778]},"properties":{'type':'8'}, "id": "03N3211"},
{'geometry':{"coordinates":[121.87635833333333, 30.86567777777778]},"properties":{'type':'5'}, "id": "01N2234"},
{'geometry':{"coordinates":[121.87635833333333, 30.86567777777778]},"properties":{'type':'8'}, "id": "03N3211"},
{'geometry':{"coordinates":[121.8758861111111, 30.866086111111112]},"properties":{'type':'32'}, "id": "01N2234"},
{'geometry':{"coordinates":[121.87635833333333, 30.86567777777778]},"properties":{'type':'8'}, "id": "09N1111"},
{'geometry':{"coordinates":[121.87635833333333, 30.86567777777778]},"properties":{'type':'11'}, "id": "03N3211"},
{'geometry':{"coordinates":[121.87635833333333, 30.86567777777778]},"properties":{'type':'2'}, "id": "09N1111"},
{'geometry':{"coordinates":[121.87705277777778, 30.86705]}, "properties": {'type': '2'}, "id": "01N2234"}
]
以上结果应该为:
draw_data = [
{'geometry':{"coordinates":[121.8758861111111, 30.866086111111112]},"properties":{'type':'32'}, "id": "01N2234"},
{'geometry':{"coordinates":[121.87635833333333, 30.86567777777778]},"properties":{'type':'8'}, "id": "09N1111"},
{'geometry':{"coordinates":[121.87635833333333, 30.86567777777778]},"properties":{'type':'11'}, "id": "03N3211"},
]
def removeduplicate(self, list1, priority=None):
"""
列表套字典去重复, 筛选相同组串id优先级最高的类型
:param list1: 输入一个有重复值的列表
:priority : 优先级列表
:return: 返回一个去掉重复的列表
"""
sort_dict = {'6': 100, '4': 99, '2': 98, '5': 97, '8': 96, '3': 95, '7': 94, '1': 93} # self.types 颜色表按优先级排序
newlist = []
print("list1:", list1)
for ind_i, i in enumerate(list1): # 先遍历原始字典
flag = True
if newlist == []: # 如果是空的列表就不会有重复,直接往里添加
pass
else:
for ind_j, j in enumerate(newlist):
j_id = j['id']
if j_id == i['id']: # 相同id
if sort_dict[j['properties']['type']] <= sort_dict[i['properties']['type']]:
newlist[ind_j] = i
else:
flag=False
else: # 不相等,id可能已经出现过
for ind_li, li in enumerate(newlist):
if i['id'] == li['id']:
if sort_dict[i['properties']['type']] >= sort_dict[li['properties']['type']]:
newlist[ind_li] = i
else:
flag = False
if flag:
newlist.append(i)
return newlist
来源:https://blog.csdn.net/Hu_helloworld/article/details/124370840
标签:python,列表套,json,字典,key
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中*args与**kwarsg及闭包和装饰器的用法
2023-07-24 00:43:34
![](https://img.aspxhome.com/file/2023/6/60956_0s.png)
如何实现在下拉菜单里输入文字?
2010-06-03 10:31:00
教你轻松学会SQL Server记录轮班的技巧
2009-02-19 17:38:00
JavaScript 解析 Cookie 的函数
2007-11-08 11:58:00
oracle 树查询 语句
2009-07-17 18:20:00
python try...finally...的实现方法
2022-09-27 06:34:35
兼容主流浏览器的【表情插入】源码
2010-07-02 13:02:00
![](https://img.aspxhome.com/file/UploadPic/20107/2/loveliness-32s.gif)
讲解SQL Server数据库触发器的安全隐患
2009-02-24 17:46:00
学习CSS布局心得
2007-05-11 16:50:00
我对SQL SERVER 存储过程见解
2009-10-31 18:51:00
HTML5设计原则
2012-04-26 16:46:45
五个常用MySQL图形化管理工具
2012-01-05 18:49:16
Go语言中defer语句的用法
2023-07-07 11:04:17
![](https://img.aspxhome.com/file/2023/3/92023_0s.png)
GoLang使goroutine停止的五种方法实例
2023-09-02 08:31:33
![](https://img.aspxhome.com/file/2023/5/101675_0s.png)
有用的SQL语句(删除重复记录,收缩日志)
2008-03-04 16:59:00
大家都对vertical-align的各说各话
2008-06-19 12:11:00
![](https://img.aspxhome.com/file/UploadPic/20086/19/2008619121517533s.gif)
javascript实现数独解法
2023-10-17 17:18:28
超级连接的title提示中如何换行实现多行显示
2008-03-07 15:57:00
Python面向对象之继承原理与用法案例分析
2021-06-28 02:07:53
![](https://img.aspxhome.com/file/2023/4/65544_0s.png)
PHP Session条件竞争超详细讲解
2023-06-03 12:49:00
![](https://img.aspxhome.com/file/2023/7/55337_0s.png)