亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在python中獲取json/dictionary中的所有關鍵路徑組合

在python中獲取json/dictionary中的所有關鍵路徑組合

一只名叫tom的貓 2021-12-21 11:07:34
我希望能夠在 JSON 文件中獲取所有各種鍵的路徑。我經常獲得大型 JSON,但我不確定各種數據元素可能在哪里?;蛘呶倚枰樵償祿母鞣N元素??梢暬?JSON 樹可能不方便。基本上,我想獲得所有不同路徑的列表,以使未來的各種任務更容易。例如:myjson = {'transportation':'car','address': {'driveway':'yes','home_address':{'state':'TX','city':'Houston'}}, 'work_address':{'state':'TX','city':'Sugarland', 'location':'office-tower', 'salary':30000}}如果我可以運行某種類型的循環來以下面的這種格式或某種格式返回列表,那就太好了。myjson['地址']['車道']myjson.address myjson.address.driveway myjson.address.home_address myjson.address.home_address.city myjson.address.home_address.state myjson.transportation myjson.work_address myjson.work_address.city myjson.work_address.location myjson.work_address.salary myjson。工作地址.state例如我已經開始mylist = []for  key, value in myjson.items():    mylist.append(key)    if type(value) is dict:        for key2, value2 in myjson[key].items():        mylist.append(key+'.'+key2)print(mylist)我想這有點工作,但我不知道如何無限期地迭代。例如,我如何將其構建為 3-10+ 層深?
查看完整描述

3 回答

?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

很棒的片段!


這是一個管理列表的版本:


def get_keys(some_dictionary, parent=None):

    if isinstance(some_dictionary, str):

        return

    for key, value in some_dictionary.items():

        if '{}.{}'.format(parent, key) not in my_list:

            my_list.append('{}.{}'.format(parent, key))

        if isinstance(value, dict):

            get_keys(value, parent='{}.{}'.format(parent, key))

        if isinstance(value, list):

            for v in value:

                get_keys(v, parent='{}.{}'.format(parent, key))

        else:

            pass


查看完整回答
反對 回復 2021-12-21
?
Smart貓小萌

TA貢獻1911條經驗 獲得超7個贊

我認為這應該滿足您的要求:


myjson = {

    'transportation': 'car',

    'address': {

        'driveway': 'yes',

        'home_address': {

            'state': 'TX',

            'city': 'Houston'}

    },

    'work_address': {

        'state': 'TX',

        'city': 'Sugarland',

        'location': 'office-tower',

        'salary': 30000}

}



def get_keys(some_dictionary, parent=None):

    for key, value in some_dictionary.items():

        if '{}.{}'.format(parent, key) not in my_list:

            my_list.append('{}.{}'.format(parent, key))

        if isinstance(value, dict):

            get_keys(value, parent='{}.{}'.format(parent, key))

        else:

            pass



my_list = []

get_keys(myjson, parent='myjson')

print(my_list)

輸出:


['myjson.transportation',

'myjson.work_address',

'myjson.work_address.city',

'myjson.work_address.state',

'myjson.work_address.location',

'myjson.work_address.salary',

'myjson.address',

'myjson.address.driveway',

'myjson.address.home_address',

'myjson.address.home_address.city',

'myjson.address.home_address.state']

關鍵是繼續get_keys()從函數內部遞歸調用!


查看完整回答
反對 回復 2021-12-21
?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

也在 json 中處理列表路徑的實現。


import json

def get_json_key_path(jsonStr, enable_index):

    json_keys = []

    jsonObj = json.loads(jsonStr)

    

    def get_key_path(jsonObj, parent=None):

        if not isinstance(json_obj, dict):

            return

        for key, value in jsonObj.items():

            if not isinstance(value, list) and '{}.{}'.format(parent, key) not in json_keys:

                json_keys.append('{}.{}'.format(parent, key))

            if isinstance(value, dict):

                get_key_path(value, parent='{}.{}'.format(parent, key))

            elif isinstance(value, list):

                i = 0

                for obj in value:

                    if enable_index:

                        get_key_path(obj, parent='{}.{}.{}'.format(parent, key, i))

                    else:

                        get_key_path(obj, parent='{}.{}'.format(parent, key))

                    i = i + 1

            else:

                pass


    get_key_path(jsonObj, "")

    return [ s[1:] for s in json_keys]


查看完整回答
反對 回復 2021-12-21
  • 3 回答
  • 0 關注
  • 322 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號