亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python?列表套json字典根據(jù)相同的key篩選數(shù)據(jù)

 更新時(shí)間:2022年04月24日 08:48:35   作者:shine?stone  
這篇文章主要介紹了python?列表套json字典根據(jù)相同的key篩選數(shù)據(jù),文章基于python的相關(guān)資料展開詳細(xì)的內(nèi)容介紹需要的小伙伴可以參考一下

前言:

工作中遇到以下小問題,解決方法如下,可能比較暴力,暫時(shí)留檔,再進(jìn)行優(yōu)化。

要求:將列表中json的 ‘id’ 字段值相同的數(shù)據(jù),根據(jù) type的值,按照一定的優(yōu)先級(jí)次序排列,列表中僅保留優(yōu)先級(jí)最高的type

測(cè)試用例:

list1 示例數(shù)據(jù):

type優(yōu)先級(jí)列表:[6, 4, 2, 5, 8, 3, 7, 1] (依次遞減,6優(yōu)先級(jí)最高,1優(yōu)先級(jí)最低)

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"}
? ? ? ? ]

以上結(jié)果應(yīng)該為:

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):
? ? ? ? """
? ? ? ? 列表套字典去重復(fù), 篩選相同組串id優(yōu)先級(jí)最高的類型
? ? ? ? :param list1: 輸入一個(gè)有重復(fù)值的列表
? ? ? ? :priority : 優(yōu)先級(jí)列表
? ? ? ? :return: 返回一個(gè)去掉重復(fù)的列表
? ? ? ? """
? ? ? ? sort_dict = {'6': 100, '4': 99, '2': 98, '5': 97, '8': 96, '3': 95, '7': 94, '1': 93} ? # self.types 顏色表按優(yōu)先級(jí)排序
? ? ? ? newlist = []
? ? ? ? print("list1:", list1)
? ? ? ? for ind_i, i in enumerate(list1): ?# 先遍歷原始字典
? ? ? ? ? ? flag = True
? ? ? ? ? ? if newlist == []: ?# 如果是空的列表就不會(huì)有重復(fù),直接往里添加
? ? ? ? ? ? ? ? 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可能已經(jīng)出現(xiàn)過
? ? ? ? ? ? ? ? ? ? ? ? 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

到此這篇關(guān)于python 列表套json字典根據(jù)相同的key篩選數(shù)據(jù)的文章就介紹到這了,更多相關(guān)python json篩選數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論