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

Python 从列表中的字典元组创建字典字典

Python 从列表中的字典元组创建字典字典,python,list,dictionary,tuples,Python,List,Dictionary,Tuples,我有一个这样的元组 sample_tuple = ([{1:["hello"], 2: ["this"], 3:["is fun"]},{1:["hi"], 2:["how are you"]}], [{1: ["this"], 2:[], 3:["that"]}, {1:[], 2:["yes"]}]) 从这个元组中,我想创建一个字典,它的键值是dictionary 第1步: 迭代主大元组并跟踪列表的索引 第二步: 进入元组中的列表并跟踪那些大列表的索引 i、

我有一个这样的元组

sample_tuple = ([{1:["hello"], 2: ["this"], 3:["is fun"]},{1:["hi"], 2:["how are you"]}],
                [{1: ["this"], 2:[], 3:["that"]}, {1:[], 2:["yes"]}])
从这个元组中,我想创建一个字典,它的键值是dictionary

第1步:

迭代主大元组并跟踪列表的索引

第二步:

进入元组中的列表并跟踪那些大列表的索引

i、 e,第一个列表的索引
0

[{1:["hello"], 2: ["this"], 3:["is fun"]},{1:["hi"], 2:["how are you"]}] 
第三步:

我想遍历列表中dictionary的键和值

i、 第一本词典

{1:["hello"], 2: ["this"], 3:["is fun"]}
第4步: 在遍历字典值时,我要检查并确保值不是空的,也不是无的

当这个过程发生时,我想创建一个字典。对于本
词典

键:步骤2的索引(大列表中每个字典的每个索引)

值:如果
步骤3
字典的值不为空,则该字典具有来自
步骤3
的键(来自上面的我的字典)的键和作为列表(棘手部分)的值。正如您在下面看到的,我有一个空列表
temporary\u keyword\u list
,它应该将非空列表值保存到一个临时列表中,但我没有得到我想要的

下面是我尝试过的,我得到的和我想要的输出

output_1 = {}
for index, each_keyword in enumerate(sample_tuple):

    for ind, each_file in enumerate(each_keyword):
        temporary_dict = {}

        for key, value in each_file.items():

            temporary_keyword_list = []
            # Check if a value is not empty or not None
            if value!= [] and value is not None:
                temporary_keyword_list.append(index) ## Here I want to save the index (tricky part)

            # Start inserting values into the dictionary. 
            temporary_dict[key] = temporary_keyword_list

        # Final big dictionary 
        output_1[ind] = temporary_dict  
我当前的
输出\u 1
字典:

{0: {1: [1], 2: [], 3: [1]}, 1: {1: [], 2: [1]}}
期望输出:

{0: {1: [0, 1], 2: [0], 3: [0, 1]}, 1: {1: [0], 2: [0, 1]}}

由于它的元组、列表和字典,我尽力解释我遇到的问题。请在评论中告诉我,如果这不合理,我会尽力解释。任何帮助或建议都会很棒

您可能不需要在此处创建临时列表或字典,因为您可以从for循环中获得所需的所有索引。这里的关键是,初始元组包含具有类似结构的列表,因此在代码中,在第一个for循环的第一次迭代之后,最终字典的结构已经确定。当您计划在内部存储列表时,也要考虑使用Debug语句。然后就是正确处理索引和值。下面的代码应该可以工作

from collections import defaultdict
sample_tuple = ([{1: ["hello"], 2: ["this"], 3: ["is fun"]},
                 {1: ["hi"], 2: ["how are you"]}],
                [{1: ["this"], 2: [], 3: ["that"]}, {1: [], 2: ["yes"]}])
output_1 = {}
for index, each_keyword in enumerate(sample_tuple):
    for ind, each_file in enumerate(each_keyword):
        if index == 0:
            output_1[ind] = defaultdict(list)
        for key, value in each_file.items():
            if value != [] and value is not None:
                output_1[ind][key].append(index)
            print(output_1)
要回答您的评论,您可以不使用defaultdict进行管理,但您真的想这样做吗

sample_tuple = ([{1: ["hello"], 2: ["this"], 3: ["is fun"]},
                 {1: ["hi"], 2: ["how are you"]}],
                [{1: ["this"], 2: [], 3: ["that"]}, {1: [], 2: ["yes"]}])
output_1 = {}
for index, each_keyword in enumerate(sample_tuple):
    for ind, each_file in enumerate(each_keyword):
        for key, value in each_file.items():
            if index == 0:
                if key == 1:
                    if value != [] and value is not None:
                        output_1[ind] = {key: [index]}
                else:
                    if value != [] and value is not None:
                        output_1[ind][key] = [index]
            else:
                if value != [] and value is not None:
                    output_1[ind][key].append(index)
            print(output_1)

这里的关键是,您的初始元组包含具有类似结构的列表,因此在您的代码中,在第一个for循环的第一次迭代之后,最终字典的结构已经确定了”
“这就是我的问题所在,我一直在尝试一步一步地进行,在我的第二次迭代中,
输出_1
被替换。
defaultdict
是解决这个问题的唯一方法吗?你现在在我眼里就像上帝一样!感谢您展示这两种方法,我将学习
defaultdictionaries
。老实说,我不知道python中存在这样的东西!