Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将子字典附加到主字典_Python_Python 3.x_Dictionary - Fatal编程技术网

Python 将子字典附加到主字典

Python 将子字典附加到主字典,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我有一个将字典值收集到子字典的函数,如何将它作为嵌套条目附加到主字典中 Dict = {} def getDetail(): subdict = {} first_name = input("Enter first name") if first_name.lower() == 'quit': return None else: last_name = input("Enter last name") nick_na

我有一个将字典值收集到子字典的函数,如何将它作为嵌套条目附加到主字典中

Dict = {}
def getDetail():
    subdict = {}
    first_name = input("Enter first name")
    if first_name.lower() == 'quit':
        return None
    else:
        last_name = input("Enter last name")
        nick_name = input("Enter nickname")
        address = input("Enter address")
        number = input("Enter phone number")
        subdict['nick'] = nick_name 
        subdict['firstname'] = first_name
        subdict['lastname'] = last_name
        subdict['address'] = address
        subdict['number'] = number
        return subdict
我希望结果返回一个嵌套条目,由
['nick']
索引,因此Dict如下所示


{'Johnny':{'firstname':'John','lastname':'Johnson','address':'103 Alpine Drive','nick':'Johnny','number':'021-233-4555'}}

你可以像平常那样做任何条目

my_dict[key] = value
采用以下格式:

my_dict = Dict
value = getDetail()
key = "Up to you"
my_dict[key] = value
因此,你可以:

Dict = {}
def getDetail():
    subdict = {}
    first_name = input("Enter first name")
    if first_name.lower() != 'quit':
        last_name = input("Enter last name")
        nick_name = input("Enter nickname")
        address = input("Enter address")
        number = input("Enter phone number")
        subdict['nick'] = nick_name 
        subdict['firstname'] = first_name
        subdict['lastname'] = last_name
        subdict['address'] = address
        subdict['number'] = number
        dict[nick_name] = subdict

或者写得简单而肮脏:

MyDict = {}
def GetDetail():    
    first_name = raw_input("Enter first name: ")
    if first_name.lower() == 'quit':
        return None

    else:
        last_name = raw_input("Enter last name: ")
        nick_name = raw_input("Enter nickname: ")
        address = raw_input("Enter address: ")
        number = raw_input("Enter phone number: ")        

        return {
            'nick':nick_name,
            'firstname':first_name,
            'lastname':last_name,
            'address':address,
            'number':number
        }


MyDict['Johnny'] = GetDetail()
print MyDict
输入和输出示例:

输入姓氏:Higazi

输入昵称:tameritoke

输入地址:MyHomeStreet

输入电话号码:000-1999


{'Johnny':{'nick':'tameritoke','address':'MyHomeStreet','number':'000-1999','firstname':'Tamer','lastname':'Higazi'}}

Dict['subct']=getDetail()
?你能澄清一下你想要的结果是什么样的吗?您是否正在尝试构建一个目录条目的dict,其中键是一个名称(如电话簿)?您是否只希望Dict是一个子目录列表?或者这是上面建议的吗?我希望子目录条目是嵌套的字典,通过Dict中的昵称索引,如下:
{'Johnny':{'firstname':'John','lastname':'Johnson','address':'103 Alpine Drive','nick':'Johnny','number':'021-233-4555'}
威尔就是这么告诉你的。我再给你添加一个样本。嘿,AJP-很高兴它能帮上忙。(如果它提供了你所需要的,你也可以正式接受答案。)