欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言:

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

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

測試用例:

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

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

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優(yōu)先級最高的類型
? ? ? ? :param list1: 輸入一個有重復值的列表
? ? ? ? :priority : 優(yōu)先級列表
? ? ? ? :return: 返回一個去掉重復的列表
? ? ? ? """
? ? ? ? sort_dict = {'6': 100, '4': 99, '2': 98, '5': 97, '8': 96, '3': 95, '7': 94, '1': 93} ? # self.types 顏色表按優(yōu)先級排序
? ? ? ? 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可能已經(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

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

相關文章

最新評論