關(guān)于python實(shí)現(xiàn)json/字典數(shù)據(jù)中所有key路徑拼接組合問(wèn)題
案例需求:
在已知的json數(shù)據(jù)或者字典數(shù)據(jù)中,獲取所有key值的路徑拼接值,并能夠根據(jù)拼接路徑獲取對(duì)應(yīng)的值。
示例代碼:
import jmespath data = { "id": 1, "name": '張三', "age": 25, "grade": { "Chinese": 99, "Math": 100 }, "records": [ { "Chinese": 98, "Math": 99 }, { "Chinese": 97, "Math": 97 } ] } # 方法一: # 下面寫法是最笨的方法,當(dāng)?shù)螖?shù)非常多時(shí),要寫好多循環(huán),代碼量也增加了 my_list = [] for key, value in data.items(): my_list.append(key) if type(value) is dict: for key2, value2 in data[key].items(): my_list.append(key + '.' + key2) print(my_list) # 方法二(遞歸實(shí)現(xiàn)): def get_keys_path(dict_data, enable_index=None): def dfs(dict_data, parent=''): if not isinstance(dict_data, dict): return for key, value in dict_data.items(): if f"{key}.{value}" not in tmp_list: tmp_list.append(f"{parent}.{key}") if isinstance(value, dict): dfs(value, parent=f"{parent}.{key}") elif isinstance(value, list): num = 0 for i in value: if enable_index: dfs(i, parent=f"{parent}.{key}[{num}]") # dfs(i, parent=f"{parent}.{key}.{num}") else: dfs(i, parent=f"{parent}.{key}") num += 1 else: pass tmp_list = [] dfs(dict_data, parent='') if enable_index: return [i[1:] for i in tmp_list] else: return [i[1:] for i in list(set(tmp_list))] # 獲取key路徑組合 print(get_keys_path(data, 1)) # 獲取key路徑值 print(jmespath.search('records[1].Chinese', data))
運(yùn)行結(jié)果:
到此這篇關(guān)于關(guān)于python實(shí)現(xiàn)json/字典數(shù)據(jù)中所有key路徑拼接組合問(wèn)題的文章就介紹到這了,更多相關(guān)python數(shù)據(jù)中key路徑拼接內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python數(shù)字圖像處理實(shí)現(xiàn)圖像的形變與縮放
這篇文章主要為大家介紹了python數(shù)字圖像處理實(shí)現(xiàn)圖像的形變與縮放示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06python 實(shí)現(xiàn)mysql增刪查改示例代碼
python中可以通過(guò)pymysql實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的連接,并實(shí)現(xiàn)數(shù)據(jù)庫(kù)的各種操作,這篇文章主要給大家介紹了關(guān)于pymsql實(shí)現(xiàn)增刪改查的示例代碼,需要的朋友可以參考下2021-11-11使用Python操作MySql數(shù)據(jù)庫(kù)和MsSql數(shù)據(jù)庫(kù)
這篇文章介紹了使用Python操作MySql數(shù)據(jù)庫(kù)和MsSql數(shù)據(jù)庫(kù)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05利用Python如何批量修改數(shù)據(jù)庫(kù)執(zhí)行Sql文件
這篇文章主要給大家介紹了關(guān)于利用Python如何批量修改數(shù)據(jù)庫(kù)執(zhí)行Sql文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07pandas進(jìn)階教程之Dataframe的apply方法
DataFrame中的apply方法就是將函數(shù)應(yīng)用到由列或行形成的一維數(shù)組上,下面這篇文章主要給大家介紹了關(guān)于pandas進(jìn)階教程之Dataframe的apply方法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09Anaconda+VSCode配置tensorflow開發(fā)環(huán)境的教程詳解
Anaconda是一個(gè)開源的python發(fā)行版本,是現(xiàn)在比較流行的python數(shù)據(jù)科學(xué)平臺(tái),可以對(duì)python的科學(xué)包做到有效管理。這篇文章主要介紹了Anaconda+VSCode配置tensorflow開發(fā)環(huán)境,需要的朋友可以參考下2020-03-03python實(shí)現(xiàn)微信定時(shí)每天和女友發(fā)送消息
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)微信定時(shí)每天和女友發(fā)送消息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Python數(shù)據(jù)獲取實(shí)現(xiàn)圖片數(shù)據(jù)提取
本文主要介紹了Python數(shù)據(jù)獲取實(shí)現(xiàn)圖片數(shù)據(jù)提取,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05