简体   繁体   English

如何从字典列表中具有相同键的连接值中获取嵌套数据?

[英]how to get a nested data from Concatenate values with same keys in a list of dictionaries?

I have all_writing_test_name_data我有 all_writing_test_name_data

all_writing_test_name_data=    
      [
          {
            "id": 1,
            "book_name": Math,
            "writing_test_description": "string",
            "subject_id": 1,
            "book_id": 2,
            "writing_test": "string"
          },
          {
            "id": 2,
            "book_name": Math-1,
            "writing_test_description": "string-1",
            "subject_id": 1,
            "book_id": 2,
            "writing_test": "string-1"
          }
        ]

and I want to Concatenate all_writing_test_name_data like this我想像这样连接 all_writing_test_name_data

 [
  {
    "subject_id": 1,
    "writing_items": [
      {
            "id": 1,
            "book_name": Math,
            "writing_test_description": "string",
            "book_id": 2,
            "writing_test": "string"
          },
          {
            "id": 2,
            "book_name": Math-1,
            "writing_test_description": "string-1",
            "book_id": 2,
            "writing_test": "string-1"
          }
    ]
  }
]

i have tried this but i think there is some lacking in the code for this i can't get the desire data我已经尝试过了,但我认为代码中缺少一些我无法获得所需数据的代码

    x=all_writing_test_name_data
    

    # printing original list
    print("The original list is : " + str(x))
    
    import operator
    from functools import reduce
    all_keys = reduce(operator.or_, (d.keys() for d in x))

    bar = {key: [d.get(key) for d in x] for key in all_keys}

    print('bar',bar['writing_test']+bar['writing_test_description'])


    from collections import Counter
    result = Counter()
    for d in x:
        result[d['writing_test']] = d['writing_test']
        result[d['writing_test_description']] = d['writing_test_description']
    print(result)

    z=bar['writing_test']+bar['writing_test_description']

    print bar

but I can't get my desire data.但我无法获得我想要的数据。 how can i get the exact same data,what is the mistake我怎样才能得到完全相同的数据,有什么错误

You can group the input data by the subject_id property of each dict , then re-format that data into the value you want:您可以按每个dictsubject_id属性对输入数据进行分组,然后将该数据重新格式化为您想要的值:

from collections import defaultdict

groups = defaultdict(list)
for test in all_writing_test_name_data:
    subject_id = test.pop('subject_id')
    groups[subject_id].append(test)

result = [ { 'subject_id' : k, 'writing_items' : v } for k, v in groups.items() ]

Output: Output:

[
    {
        "subject_id": 1,
        "writing_items": [
            {
                "id": 1,
                "book_name": "Math",
                "writing_test_description": "string",
                "book_id": 2,
                "writing_test": "string"
            },
            {
                "id": 2,
                "book_name": "Math-1",
                "writing_test_description": "string-1",
                "book_id": 2,
                "writing_test": "string-1"
            }
        ]
    }
]

Note that this will alter the value of all_writing_test_name_data (due to the test.pop() ).请注意,这将改变all_writing_test_name_data的值(由于test.pop() )。 If this is not desired, add如果不需要,请添加

test = test.copy()

prior to the pop .pop之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM