python 列表套json字典根据相同的key筛选数据
时间:2022-06-15 10:20:49|栏目: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