如何在列表中获取嵌套字典的值?

时间:2017-06-02 20:44:20

标签: python list dictionary nested

我遇到了一个小python函数的问题,目前我有这样的结构:

dict_conf = {'conf_storage':
            {'option1':[
                {'number':'20169800'},
                {'name':'usb'},
                {'description':'16gb'},
                {'qty':'1'},
                {'vendor=':'XLR'},
                {'online':'Yes'}],
            'option2':[
                {'number':'20161789'},
                {'name':'hardrive'},
                {'description':'128gb'},
                {'qty':'1'},
                {'vendor=':'KBW'},
                {'online':'NO'}]},
        'conf_grph':
                {'option1':[
                    {'number':'20170012'},
                    {'name':'HD_screen'},
                    {'description':'1080p'},
                    {'qty':'1'},
                    {'vendor=':'PWD'},
                    {'online':'Yes'}]}}

conf_type = raw_input("Enter the conf type: ")
option = raw_input("Enter the option")

我想找"数字"值,例如,如果用户输入:

conf_type = "conf_storage"
number = "20169800"

然后打印值和一条消息说:"您输入的是有效号码,它是:20169800"

我的想法是迭代并返回与用户输入的值相等的每个值。

如果我使用iteritems我得到每个元素然后我可以将它放入for循环但是之后我不知道如何进入包含字典的列表并检索&#34的值;数"键。

如果你有答案,请你解释一下,我的意思是你是怎么做的。

由于

4 个答案:

答案 0 :(得分:1)

一个简单的解决方案可能是迭代所有元素。

conf_type = "conf_storage"
number = "20169800"

if dict_conf[conf_type]:
    for key, value in dict_conf[conf_type].items():
        for v in value:
            for k,num in v.items():
                if num == number:
                    print('found')

答案 1 :(得分:0)

这应该这样做:

print "You entered a valid number, it is:", dict_conf[conf_type][option][0]['number']

https://repl.it/I3tr

答案 2 :(得分:0)

numbers转换为列表并检查输入的number是否在列表中,例如:

conf_type = "conf_storage"
number = "20169800"

if number in [option[0]['number'] for option in dict_conf[conf_type].values()]:
    print("You entered a valid number, it is: {}".format(number))
# You entered a valid number, it is: 20169800

答案 3 :(得分:0)

这是完整的工作代码:

dict_conf = {'conf_storage':
            {'option1':[
                {'number':'20169800'},
                {'name':'usb'},
                {'description':'16gb'},
                {'qty':'1'},
                {'vendor=':'XLR'},
                {'online':'Yes'}],
            'option2':[
                {'number':'20161789'},
                {'name':'hardrive'},
                {'description':'128gb'},
                {'qty':'1'},
                {'vendor=':'KBW'},
                {'online':'NO'}]},
    'conf_grph':
            {'option1':[
                {'number':'20170012'},
                {'name':'HD_screen'},
                {'description':'1080p'},
                {'qty':'1'},
                {'vendor=':'PWD'},
                {'online':'Yes'}]}}


conf_type = raw_input("Enter the conf type: ")
option = raw_input("Enter the option: ")
number = raw_input("Enter the number for validation: ")

dict_options = dict_conf[conf_type]
option_list = dict_options[option]

for elem_dict in option_list:
    if 'number' in elem_dict.keys():
        if elem_dict['number'] == number:
            print "You entered a valid number, it is: " + number