前言:
工作中遇到以下小问题,解决方法如下,可能比较暴力,暂时留档,再进行优化。
要求:将列表中json的 ‘id’ 字段值相同的数据,根据 type的值,按照一定的优先级次序排列,列表中仅保留优先级最高的type
。
测试用例:
list1 示例数据:
type优先级列表:[6, 4, 2, 5, 8, 3, 7, 1] (依次递减,6优先级最高,1优先级最低)
1
2
3
4
5
6
7
8
9
10
|
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" } ] |
以上结果应该为:
1
2
3
4
5
|
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" }, ] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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