python?列表套json字典根據(jù)相同的key篩選數(shù)據(jù)
前言:
工作中遇到以下小問題,解決方法如下,可能比較暴力,暫時(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)文章
詳解Python中的__getitem__方法與slice對(duì)象的切片操作
Python中想要使類的實(shí)例像list一樣使用下標(biāo),可以用__getitem__方法,而配合slice對(duì)象則可以實(shí)現(xiàn)list一樣的切片,詳解Python中的__getitem__方法與slice對(duì)象的切片操作2016-06-06Python學(xué)習(xí)小技巧之列表項(xiàng)的拼接
這篇文章主要給大家介紹了Python學(xué)習(xí)小技巧之列表項(xiàng)的拼接的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05Django 開發(fā)調(diào)試工具 Django-debug-toolbar使用詳解
這篇文章主要介紹了Django 開發(fā)調(diào)試工具 Django-debug-toolbar使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07Python/R語言分別實(shí)現(xiàn)斐波那契數(shù)列的示例詳解
這篇文章將通過兩個(gè)小問題:年齡計(jì)算、斐波那契數(shù)列,帶領(lǐng)大家深入淺出的理解兩種語言的基本語法,并用以實(shí)際場(chǎng)景,需要的可以參考一下2022-03-03使用Python測(cè)試Ping主機(jī)IP和某端口是否開放的實(shí)例
今天小編就為大家分享一篇使用Python測(cè)試Ping主機(jī)IP和某端口是否開放的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12python GUI庫圖形界面開發(fā)之PyQt5打開保存對(duì)話框QFileDialog詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5打開保存對(duì)話框QFileDialog詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-02-02