python?列表套json字典根據(jù)相同的key篩選數(shù)據(jù)
前言:
工作中遇到以下小問題,解決方法如下,可能比較暴力,暫時留檔,再進行優(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"}
? ? ? ? ]以上結(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)先級最高的類型
? ? ? ? :param list1: 輸入一個有重復(fù)值的列表
? ? ? ? :priority : 優(yōu)先級列表
? ? ? ? :return: 返回一個去掉重復(fù)的列表
? ? ? ? """
? ? ? ? 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 == []: ?# 如果是空的列表就不會有重復(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Python中的__getitem__方法與slice對象的切片操作
Python中想要使類的實例像list一樣使用下標(biāo),可以用__getitem__方法,而配合slice對象則可以實現(xiàn)list一樣的切片,詳解Python中的__getitem__方法與slice對象的切片操作2016-06-06
Django 開發(fā)調(diào)試工具 Django-debug-toolbar使用詳解
這篇文章主要介紹了Django 開發(fā)調(diào)試工具 Django-debug-toolbar使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07
Python/R語言分別實現(xiàn)斐波那契數(shù)列的示例詳解
這篇文章將通過兩個小問題:年齡計算、斐波那契數(shù)列,帶領(lǐng)大家深入淺出的理解兩種語言的基本語法,并用以實際場景,需要的可以參考一下2022-03-03
python GUI庫圖形界面開發(fā)之PyQt5打開保存對話框QFileDialog詳細(xì)使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5打開保存對話框QFileDialog詳細(xì)使用方法與實例,需要的朋友可以參考下2020-02-02

