簡體   English   中英

使用嵌套列表或來自 2 個列表的字典創建分層字典

[英]Creating a hierarchical dictionary with nested lists or dictionaries from 2 lists

我有 2 個清單。 第一個有數字,表示同一索引的另一個列表中的值屬於層次結構的哪一部分。 我正在嘗試獲取理論上可以 go 到任意數量的步驟的動態代碼。

前 3 個工作正常,但是當我有更多的層次結構步驟時,我無法獲得完美的回報。 我只是在學習 Python 所以如果有任何建議可以改進我的丑陋代碼,我將不勝感激。

第一個列表是一個包含數字的列表。 這些數字是層次結構中的步驟。 第二個列表將根據第一個列表在其共享索引處的步長值進行排序。

以下是我正在嘗試的 3 個示例。 前 2 個是正確的,但它們僅適用於第 0 步和第 1 步。我正在嘗試使代碼適用於任何長度的列表和任何數字范圍(接下來的步驟只能是 +/- 1)。

output 應該如下所示,使用顯示步驟的第一個列表:

0
0
 1
 1
  2
   3
  2
  2
 1
0
 1
  2
   3
   3

我想將所有 0 步驟設置為第二個列表的相同索引值的字典。 該字典將包含嵌套列表或嵌套字典。 我不確定哪個是 go 的更好方法。

對於任何混淆,我深表歉意,並感謝您的幫助。

#!/bin/usr/env python

from pprint import pprint

print('\n')

list1 = [0, 1, 0, 1, 1]
lista = ['a','b','c', 'd', 'e']
dict = {}
for index, value in enumerate(lista):
  inc = 1
  if (list1[index] == 0):
     dict[value] = []
     try:
        while (list1[index + inc] > 0):
            dict[value].append(lista[index + inc])
            inc += 1
     except IndexError:
        pass

pprint(dict)

print('\n' + '#'*150 + "\n")

list2 = [0, 1, 1, 1, 1, 0, 1]
listb = ['a','b','c','d','e', 'f', 'g']
dict = {}
for index, value in enumerate(listb):
   inc = 1
    if (list2[index] == 0):
        dict[value] = []
        try:
            while (list2[index + inc] > 0):
                dict[value].append(listb[index + inc])
                inc += 1
        except IndexError:
            pass

pprint(dict)

print('\n' + '#'*150 + "\n")

list3 = [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 2]
listc = ['a', 'b', 'c', 'd', 'e' ,'f' , 'g', 'h', 'i', 'j', 'k', 'l', 'm' ,'n' ,'o' , 'p', 'q' ,'r' ,'s', 't', 'u', 'v', 'w', 'x']

dict = {}
temp_dict = {}
for index, value in enumerate(listc):
    inc = 1
    if (list3[index] == 0):
       dict[value] = []
        try:
            while (list3[index + inc] > 0):
                if (list3[index + inc] < list3[index + (inc + 1)]):
                    dict[value].append([listc[index + inc]])
                elif (list3[index + inc] == list3[index + (inc + 1)]):
                    dict[value].append()
                inc += 1
        except IndexError:
            pass

pprint(dict)

print('\n' + '#'*150 + "\n")

這是當前的 output。

{'a': ['b'], 'c': ['d', 'e']}

######################################################################################################################################################

{'a': ['b', 'c', 'd', 'e'], 'f': ['g']}

######################################################################################################################################################

{'a': [['b']], 'v': [['w']]}

######################################################################################################################################################

我試圖讓第三個 output 看起來像這樣。

{
'a': [ 'b', 
          [ 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u'], 
'v': [ 'w', 
          [ 'x']] 
}

這是一個解決方案。 請注意,除了實際結果之外,還會返回列表中消耗了多少項目。 如果你願意,你當然可以忽略它。

def build_dict_from_lists(depth_list, item_list):
    if len(depth_list) != len(item_list):
        raise Exception("Canincompatible lengths")

    depth0 = depth_list[0]

    res = []
    next_index = 0
    while next_index < len(depth_list) and  depth_list[next_index] >= depth0:
        item = item_list[next_index]
        depth = depth_list[next_index]        
        if depth == depth0:
            res.append(item)
            next_index += 1
        else: 
            consumed, sub_res = build_dict_from_lists(depth_list[next_index:], item_list[next_index:])
            res.append(sub_res) 
            next_index += consumed 

    if depth0 == 0 and any(isinstance(x, list) for x in res): # switch the result from a dict to a list
        d = {}
        for inx in range(0, len(res), 2):
            key = res[inx]
            val = res[inx + 1]
            d[key] = val
        res = d 

    return next_index, res

一些測試來驗證代碼的正確性:

assert build_dict_from_lists([0, 0, 0], ["a", "b", "c"]) == (3, ['a', 'b', 'c'])

assert build_dict_from_lists([0, 1, 2], ["a", "b", "c"]) == (3, {'a': ['b', ['c']]})
assert build_dict_from_lists([0, 1, 2, 1, 2], ["a", "b", "c", "d", "e"]) == (5, {'a': ['b', ['c'], 'd', ['e']]})

list3 = [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 2]
listc = ['a', 'b', 'c', 'd', 'e' ,'f' , 'g', 'h', 'i', 'j', 'k', 'l', 'm' ,'n' ,'o' , 'p', 'q' ,'r' ,'s', 't', 'u', 'v', 'w', 'x']

expected = {'a': 
[ 'b', 
          [ 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u']], 
'v': [ 'w', 
          [ 'x']] 
}
assert build_dict_from_lists(list3, listc) == (24, expected)

list4 = [0, 1, 1,     2,   2, 2,   2, 1,  2, 1, ] 
listd = ['a','b','c','d','e','f','g','h','i','j'] 
assert build_dict_from_lists(list4, listd) == (10, {'a': ['b', 'c', ['d', 'e', 'f', 'g'], 'h', ['i'], 'j']})

list5 = [0,   1,  1,  2,  2,  3,  3,  1,  1,  2,  2,  3] 
liste = ['a','b','c','d','e','f','g','h','i','j','k','l'] 
assert build_dict_from_lists(list5, liste) == (12, {'a': ['b', 'c', ['d', 'e', ['f', 'g']], 'h', 'i', ['j', 'k', ['l']]]})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM