创建新列表并追加到字典

时间:2019-12-06 09:00:00

标签: python python-3.x list dictionary

我正在尝试学习python。假设我有以下两个dict()。

  • 在第一个字典中,它包括用户信息和报告行结构。
  • 在第二个字典中,它包括属于每个人的物品计数。

我想再次比较这两个字典并总结项目总数,然后在name_S下显示结果。结果如下:

data_set_1 = { 'id': 'mid',
                'name': 'name_S',
                'directory': [
                    {
                        'id': 'eid_A',
                        'name': 'name_A',
                        'directory': []
                    },
                    { 'id': 'eid_B',
                      'name': 'name_B',
                      'directory': []
                    },
                    { 'id': 'eid_C',
                      'name': 'name_C',
                      'directory': [
                                  {'id': 'eid_C1',
                                  'name': 'name_C1',
                                  'directory': []},
                                  {'id': 'eid_C2',
                                  'name': 'name_C2',
                                  'directory': []}]
                    }]}

data_set_2 = { 'eid_A': 5,
               'eid_F': 3,
               'eid_G': 0,
               'eid_C': 1,
               'eid_C1': 10,
               'eid_C2': 20 
              }

结果:

{'name_S': 36}

如果我这样做,我就能得到结果:

def combine_compare(data_set_1, data_set_2):
    combine_result = dict()

    combine_id = data_set_1['id']
    combine_name = data_set_1['name']
    combine_directory = data_set_1['directory']

    if combine_directory:
        combine_item_sum = 0 
        combine_item_count = data_set_2.get(combine_id, 0)

        for combine_user in combine_directory:
            # Recursion starts
            for i in [combine_compare(combine_user, data_set_2)]:

                for key, value in i.items():
                    combine_item_sum += value

        combine_result[combine_name] = combine_item_sum + combine_item_count
    else:
        combine_result[combine_name] = data_set_2.get(combine_id, 0)

    return combine_result 

现在,如果我想在最终结果中包含具有项目计数的ID,则如下所示:

#if there is result and directory is not None under name_S
{'name_S': [36, ('eid_A', 'eid_C', eid_C1', 'eid_C2')]}

#if there is no result and directory is not None under name_S, display a default str
{'name_S': [0, ('Null')]}

#if there is result and directory is None under name_S, display name_S id
{'name_S': [1, ('mid')]}

我本来的想法是创建一个列表,并附加计数和ID,但是我在努力实现这一目标。这是我尝试的方法,但是列表未返回期望的结果,并且不确定如何将计数添加到列表中。任何帮助将不胜感激。

def combine_compare(data_set_1, data_set_2):
    combine_result = dict()

    # Creating a new list 
    combine_list = list()

    combine_id = data_set_1['id']
    combine_name = data_set_1['name']
    combine_directory = data_set_1['directory']

    if combine_directory:
        combine_item_sum = 0 
        combine_item_count = data_set_2.get(combine_id, 0)

        for combine_user in combine_directory:
            # Recursion starts
            for i in [combine_compare(combine_user, data_set_2)]:

                for key, value in i.items():
                    combine_item_sum += value

            # Trying to append the ids where count > 0
            if data_set_2.get(combine_user['id'], 0) != 0:
              combine_list.append(combine_user['id'])

        combine_result[combine_name] = combine_item_sum + combine_item_count
    else:
        combine_result[combine_name] = data_set_2.get(combine_id, 0)

    # Trying to print the list to see the results
    print(combine_list)

    return combine_result 

1 个答案:

答案 0 :(得分:0)

所以我从您的问题中了解到的是,您想知道data_set_1中提到的哪些id在data_set_1中以及它们的总和。以下代码是我解决上述问题的版本。

data_set_1 = { 'id': 'mid',
                'name': 'name_S',
                'directory': [
                    {
                        'id': 'eid_A',
                        'name': 'name_A',
                        'directory': []
                    },
                    { 'id': 'eid_B',
                      'name': 'name_B',
                      'directory': []
                    },
                    { 'id': 'eid_C',
                      'name': 'name_C',
                      'directory': [
                                  {'id': 'eid_C1',
                                  'name': 'name_C1',
                                  'directory': []},
                                  {'id': 'eid_C2',
                                  'name': 'name_C2',
                                  'directory': []

                                  }
                                ]
                    }
                ]
            }

data_set_2 = { 'eid_A': 5,
               'eid_F': 3,
               'eid_G': 0,
               'eid_C': 1,
               'eid_C1': 10,
               'eid_C2': 20 
              }
value = 0
final_result={}
def compare(d1,d2):
    global value
    temp_result={}
    if d1['name'] not in temp_result:
        temp_result[d1['name']]=[]

    for items in d1['directory']:
        if items['directory']:
            temp_value=compare(items,d2)
            temp_result[d1['name']].append(temp_value)
        result=check_for_value(items,d2)
        if result:
            value+=result
            temp_result[d1['name']].append(items['id'])
    return temp_result

def check_for_value(d,d2):
    if d['id'] in d2:    
        return d2[d['id']]

final_result=compare(data_set_1,data_set_2)
final_result['value']=value
print("final_result:",final_result)

哪个会给您输出dict,告诉您这些ID确切以哪个名称命名。

输出如下:

final_result: {'value': 36, 'name_S': ['eid_A', {'name_C': ['eid_C1', 'eid_C2']}, 'eid_C']}

您可以更改最终结果的结构,以使其完全符合您的期望。如果您在理解该程序时遇到任何困难,请告诉我。