如何在创建嵌套字典后访问它们的元素

时间:2018-03-27 21:19:35

标签: python

我在字典下面创建了这个,并希望输出如下:

main_dict = {
    'pins': 
        {
            'Category': ['General'], 
            'Contact': ['Mark'], 
            'Description': ['This', 'is', 'a']
        }, 
    'nails': 
        {
            'Category': ['specific'], 
            'Contact': ['Jon'], 
            'Description': ['This', 'is', 'a', 'description']
        },
    'board': 
        {
            'Category': ['General'], 
            'Contact': ['Mark'], 
            'Description': ['This', 'is', 'a']
        }, 
    'hammer': 
        {
            'Category': ['tools'], 
            'Contact': ['Jon'], 
            'Description': ['This', 'is', 'a', 'description']
        }
}

输出:     {' General':[' pins',' board'],'具体':[' nails'], '工具':['锤子']}

2 个答案:

答案 0 :(得分:0)

这是我解决问题的方法:

        mainDict={'firstKey': {'field1': ['value1'], 'field2': ['value2']}, 'secondKey': {'field1': ['valuex'], 'field2': ['valuey']}}
        x = 'field1' # by x i assume some string that matches the key field1
        myListDict = {} # empty dictionary to store the variables
        for key in  mainDict.keys():
            for subkey in mainDict[key].keys() :
                if subkey == x :
                    myListDict[mainDict[key][subkey][0]] = key

        print(myListDict)

输出是:

{'valuex': 'secondKey', 'value1': 'firstKey'}

这是基于我对所提问题或给出的信息的理解。 希望这会有所帮助。

答案 1 :(得分:0)

根据您的意见,我得到了一个似乎符合您要求的答案。首先,有问题的词典:

main_dict = {
        'pins': 
            {
                'Category': ['General'], 
                'Contact': ['Mark'], 
                'Description': ['This', 'is', 'a']
            }, 
        'nails': 
            {
                'Category': ['specific'], 
                'Contact': ['Jon'], 
                'Description': ['This', 'is', 'a', 'description']
            },
        'board': 
            {
                'Category': ['General'], 
                'Contact': ['Mark'], 
                'Description': ['This', 'is', 'a']
            }, 
        'hammer': 
            {
                'Category': ['tools'], 
                'Contact': ['Jon'], 
                'Description': ['This', 'is', 'a', 'description']
            }
}

现在,我假设'Category'始终是我们想要第一个元素的列表。如果情况并非如此,解决方案将无法正常运作。

从这里开始,我们应该声明Category作为我们关心的密钥(在您的初始示例中为X),并创建一个我们将在最后返回的answer字典。

KEY = 'Category'
answer = {}

接下来,我们将创建一个为我们的答案词汇提供数据的函数。

for elem in main_dict:
    to_add = ' '.join(main_dict[elem][KEY])
    if to_add not in answer:
        answer[to_add] = [elem]
    else:
        answer[to_add].append(elem)

最后,我们使用print(answer)打印结果并获得以下输出:

{'General': ['pins', 'board'], 'specific': ['nails'], 'tools': ['hammer']}

希望这有助于解决您的问题!如果有任何不清楚或需要进一步说明,请告诉我。

编辑:添加新答案:

construction = {'General': ['pins', 'board'], 'specific': ['nails'], 'tools': ['hammer', 'screwdriver', 'tape' ]}

longest_arr = 0
for elem in construction:
    if len(construction[elem]) > longest_arr:
        longest_arr = len(construction[elem])

lines_to_print = [[] for x in range(longest_arr)]

print(lines_to_print)
for elem in construction:
    construction[elem] = construction[elem] + [" "] * (longest_arr - len(construction[elem]))
    for index, item in enumerate(construction[elem]):
        lines_to_print[index].append(item)

for arr in lines_to_print:
    print (' '.join(arr))
相关问题