Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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_List_Dictionary - Fatal编程技术网

Python 为什么我的函数只保留一个字典键?

Python 为什么我的函数只保留一个字典键?,python,list,dictionary,Python,List,Dictionary,每当我尝试在组\u name或字段\u name中添加多个项目时,它只保留我在输入()中输入的最后一个项目。如何使我可以有多个组名或字段名 谢谢。问题在于代码重置列表,而不是在执行此操作之前正确检查正确的条件。它还将重置整个组名:如果要重新输入相同的组名,以添加更多字段,则此操作无效,您将丢失旧条目 为了解决这个问题,下面的代码仅在组名键之前不存在的情况下初始化该键。类似地,它只会在第一次初始化空列表(当\uuuuuu key\uuuuu不存在时),允许您附加其他条目并保留它们 下面是代码的注释

每当我尝试在
组\u name
字段\u name
中添加多个项目时,它只保留我在
输入()中输入的最后一个项目。如何使我可以有多个
组名
字段名


谢谢。

问题在于代码重置列表,而不是在执行此操作之前正确检查正确的条件。它还将重置整个
组名
:如果要重新输入相同的
组名
,以添加更多字段,则此操作无效,您将丢失旧条目

为了解决这个问题,下面的代码仅在
组名
键之前不存在的情况下初始化该键。类似地,它只会在第一次初始化空列表(当
\uuuuuu key\uuuuu
不存在时),允许您附加其他条目并保留它们

下面是代码的注释版本:

d = {}
while True: 
    group_name = input("Enter group name (empty to cancel): ")
    if len(group_name) == 0:
        print('')
        break
    else:
        d.[group_name] = {}
        while True:
            field_name = input("Enter field name (empty to stop): ")
            if len(field_name) == 0:
                print('')
                break
            else:
                global d_list
                d[group_name]['_keys_'] = []
                d[group_name]['_keys_'].append(field_name)
一个样本运行:

d = {}
while True:
    group_name = input("Enter group name (empty to cancel): ")
    if len(group_name) == 0:
        print('')
        break
    else:
        if group_name not in d:
            # only initialize a new group_name key
            d[group_name] = {}

        while True:
            field_name = input("Enter field name (empty to stop): ")
            if len(field_name) == 0:
                print('')
                break
            else:
                global d_list
                # similarly, only initialize if not there already
                if '_keys_' not in d[group_name]:
                    d[group_name]['_keys_'] = []
                d[group_name]['_keys_'].append(field_name)

因为每次你想添加任何东西时都会清除它:
d[group\u name][''keys][]=[]
我只看到一个问题。您正在为每个参数重新初始化键。行
d[group\u name][''keys\u']=[]
d[group\u name]={}这个语法是错误的。谢谢你们,我明白你们在说什么了。我如何每次创建一个新的而不是清除它?dict[\u keys][/code>从何而来?我想我以前从未见过这种情况。
Enter group name (empty to cancel): alfa
Enter field name (empty to stop): a
Enter field name (empty to stop): b
Enter field name (empty to stop): c
Enter field name (empty to stop): 

Enter group name (empty to cancel): beta
Enter field name (empty to stop): d
Enter field name (empty to stop): e
Enter field name (empty to stop): f
Enter field name (empty to stop): 

Enter group name (empty to cancel): 

{'alfa': {'_keys_': ['a', 'b', 'c']}, 'beta': {'_keys_': ['d', 'e', 'f']}}