好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

python 列表套json字典根据相同的key筛选数据

前言:

工作中遇到以下小问题,解决方法如下,可能比较暴力,暂时留档,再进行优化。

要求: 将列表中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筛选数据内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

查看更多关于python 列表套json字典根据相同的key筛选数据的详细内容...

  阅读:44次