第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從最后一列向動(dòng)態(tài)樹(shù)添加大小

從最后一列向動(dòng)態(tài)樹(shù)添加大小

千萬(wàn)里不及你 2022-06-22 15:31:32
我需要?jiǎng)?chuàng)建一個(gè)嵌套的 dict 結(jié)構(gòu),其中子級(jí)的數(shù)量可以在每個(gè)級(jí)別有所不同。將“大小”元素附加到旭日形圖的最后一個(gè) json 子元素 這個(gè)問(wèn)題涵蓋了樹(shù)的創(chuàng)建,除了我需要從最后一列中選取大小。鑒于我的標(biāo)簽在級(jí)別之間重復(fù),并且每個(gè)級(jí)別都可以具有與終端級(jí)別相同的標(biāo)簽“abc”,以及下一級(jí)的父級(jí) - 我在這里稍微修改了代碼(以避免在子分支中重復(fù))。但是,我無(wú)法指定存儲(chǔ)在最后一列中的大小,并且應(yīng)該替換每個(gè)葉端的 1。我知道我需要將行中的值傳遞給遞歸循環(huán) build_leaf,但似乎無(wú)法弄清楚如何。import csvfrom collections import defaultdictimport jsondef ctree():    return defaultdict(ctree)def build_leaf(name, leaf):            if len(name)==0:        res={"name":"last node"}        res['size']=1    else:        res = {"name": name}        # add children node if the leaf actually has any children        if len(leaf.keys())>0:            res["children"] = [build_leaf(k, v) for k, v in leaf.items()]        else:            res['size'] = 1    return resdef main():    tree = ctree()    # NOTE: you need to have test.csv file as neighbor to this file    with open('./inpfile.csv') as csvfile:        reader = csv.reader(csvfile)        header = next(reader)  # read the header row                i=0        for row in reader:            # usage of python magic to construct dynamic tree structure and            # basically grouping csv values under their parents            leaf = tree[row[0]]            size=row[-1]            for value in row[1:-1]:                leaf = leaf[value]    # building a custom tree structure    res = []    for name, leaf in tree.items():        res.append(build_leaf(name, leaf))    # printing results into the terminal    print(json.dumps(res, indent=2))    with open('paths.json', 'w') as fp:        json.dump(res, fp)main()所提及數(shù)據(jù)的最終輸出應(yīng)類似于:[  {    "name": "A1",    "children": [      {        "name": "A2",        "children": [          {            "name": "A1",            "children": [              {                "name": "A2",                "children": [                  {                    "name": "A3",                    "size": 80                  }                ]              }            ]          },  
查看完整描述

1 回答

?
翻過(guò)高山走不出你

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊

萬(wàn)一有人偶然發(fā)現(xiàn)了同樣的問(wèn)題——我可以通過(guò)創(chuàng)建另一個(gè)遞歸循環(huán)來(lái)從嵌套的葉子中檢索大小(感謝道格拉斯的幫助)來(lái)讓它工作。


def ctree():

    return defaultdict(ctree)


def get_size(leaf1):

    for k,v in leaf1.items():

        if k=="size":

            return v

        else:

            return get_size(v)



def build_leaf(name, leaf):


    if len(name)==0:

        res={"name":"exit site"}

        res['size']=int(get_size(leaf))


    else:

        res = {"name": name}


        # add children node if the leaf actually has any children

        if not leaf["size"]:

            res["children"] = [build_leaf(k, v) for k, v in leaf.items() if not k == "size" ]

        else:

            res['size'] = int(get_size(leaf))


    return res


def make_json(inpfile,outjson):

    tree = ctree()

    # NOTE: you need to have test.csv file as neighbor to this file

    with open("./filepath.csv") as csvfile:

        reader = csv.reader(csvfile)

        header = next(reader)  # read the header row        


        for row in reader:

            # usage of python magic to construct dynamic tree structure and

            # basically grouping csv values under their parents

            leaf = tree[row[0]]

            size=row[-1]


            for value in row[1:-1]:

                leaf = leaf[value]

            if len(row) < 6:

                leaf["exit site"]["size"]=size

            else:

                leaf["size"]=size


    # building a custom tree structure

    res = []


    for name, leaf in tree.items():

        res.append(build_leaf(name, leaf))


    with open(outjson, 'w') as fp:

        json.dump(res, fp)


查看完整回答
反對(duì) 回復(fù) 2022-06-22
  • 1 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)