将键值对列表转换为嵌套字典。

huangapple go评论69阅读模式
英文:

Convert list of key/value pairs to nested dictionary

问题

以下是您期望的输出所需的代码部分翻译:

from collections import defaultdict

data = [
    ['QR', ''],
    ['Cust', ''],
    ['fea', 'restroom'],
    ['chain', 'pa'],
    ['store', 'cd'],
    ['App', ''],
    ['End', 'EndnR'],
    ['Request', '0'],
    ['Sound', '15'],
    ['Target', '60'],
    ['Is', 'TRUE']
]

res = defaultdict(dict)
current_dict = res

for key, value in data:
    if value == '':
        current_dict = current_dict[key]
    else:
        current_dict[key] = value

print(dict(res))

这段代码将生成所需的嵌套字典结构,并输出期望的结果。

英文:

Input data:

data = [
    ['QR', ''],
    ['Cust', ''],
    ['fea', 'restroom'],
    ['chain', 'pa'],
    ['store', 'cd'],
    ['App', ''],
    ['End', 'EndnR'],
    ['Request', '0'],
    ['Sound', '15'],
    ['Target', '60'],
    ['Is', 'TRUE']
]

I want to turn this into a dictionary, and each blank value indicates the start of a new, nested sub-dictionary.

Desired output:

{
    'QR': {
            'Cust': { 
                'fea': 'restroom ',
                'chain': 'pa',
                'store': 'cd'
            },
            'App': {
                'End': 'EndnR',
                'Request': '0',
                'Sound': '15',
                'Target': '60',
                'Is': 'true'
            },
    }
}

Here is my code so far:

from collections import defaultdict
res = defaultdict(dict)
for i in data:
    res[i[0]] = i[1]
print(res)

But it only creates a flat dictionary with some blank values, not a nested dictionary.

答案1

得分: -1

以下是您要翻译的代码部分:

尝试这个

    result = {}
    nbr_keys = 0
    keys = [item[0] for item in data if item[1] == ""]
    
    for index, item in enumerate(data):
        
        if index == 0:
            if item[1] == "":
                key = item[0]
                di[item[0]] = {}
        else:
            
            if item[1] == "":
                di[key].update({item[0]: {}})
                nbr_keys += 1
                
            else:
                di[key][keys[nbr_keys]].update({item[0]: item[1]})

这将输出

    {'QR': {'Cust': {'fea': 'restroom', 'chain': 'pa', 'store': 'cd'},
      'App': {'End': 'EndnR',
       'Request': '0',
       'Sound': '15',
       'Target': '60',
       'Is': 'TRUE'}}}
英文:

try this:

result = {}
nbr_keys = 0
keys = [ item[0] for item in data if item[1] == "" ]

for index, item in enumerate(data):
    
    if index == 0:
        if item[1] == "":
            key = item[0]
            di[item[0]] = {}
    else:
        
        if item[1] == "":
            di[key].update({item[0]: {}})
            nbr_keys +=1
            
        else:
            di[key][keys[nbr_keys]].update({item[0]: item[1]})

which outputs this:

{'QR': {'Cust': {'fea': 'restroom', 'chain': 'pa', 'store': 'cd'},
  'App': {'End': 'EndnR',
   'Request': '0',
   'Sound': '15',
   'Target': '60',
   'Is': 'TRUE'}}}

huangapple
  • 本文由 发表于 2023年1月9日 16:49:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定