当前位置:
首页 > Python基础教程 >
-
python 列表套json字典根据相同的key筛选数据
这篇文章主要介绍了python 列表套json字典根据相同的key筛选数据,文章基于python的相关资料展开详细的内容介绍需要的小伙伴可以参考一下
前言:
工作中遇到以下小问题,解决方法如下,可能比较暴力,暂时留档,再进行优化。
要求:将列表中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
到此这篇关于python 列表套json字典根据相同的key筛选数据的文章就介绍到这了,更多相关python json筛选数据内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/Hu_helloworld/article/details/124370840
栏目列表
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
开启SQLSERVER数据库缓存依赖优化网站性能
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比