简体   繁体   English

如何在不使用多个循环的情况下从字典列表中获取每个记录的单独字典?

[英]How to get separate dictionary of each record from list of dictionaries without using multiple loop?

In current data "children" key will be fix.在当前数据中, "children"键将被修复。 If there is any child data available then there then it must in list of dictionary format .如果有任何可用的子数据,那么它必须在字典格式列表中

If there is no any children available then it no "children" key is available in dictionary.如果没有任何可用的"children"项,则字典中没有可用的"children"键。

I don't want to use the loop to bifurcate this data.我不想使用循环来分叉这些数据。 I want the same consistent sequence data.我想要相同的一致序列数据。 Please note there will any number of hierarchy available.请注意,将有任意数量的层次结构可用。

I want all this data in list of dictionary format like given requirement data example.我希望所有这些数据都在字典格式列表中,例如给定的需求数据示例。

Current data.当前数据。

{
    "id": 2,
    "parent_id": 1,
    "name": "0",
    "is_active": true,
    "position": 1,
    "level": 1,
    "children": [
        {
            "id": 8,
            "parent_id": 1,
            "name": "01",
            "is_active": false,
            "position": 1,
            "level": 2,
            "children": [
                "id": 9,
                "parent_id": 1,
                "name": "010",
                "is_active": false,
                "position": 1,
                "level": 2,
                "children": [
                    <'Here N number of hirerchy availabe'>
                ]
            ]
        },

    ],
    "id": 3,
    "parent_id": 1,
    "name": "1",
    "is_active": true,
    "position": 1,
    "level": 1,
    "children": [
        {
            "id": 5,
            "parent_id": 1,
            "name": "03",
            "is_active": false,
            "position": 1,
            "level": 2,
            "children": [
                "id": 6,
                "parent_id": 1,
                "name": "030",
                "is_active": false,
                "position": 1,
                "level": 2,
                "children": [
                    <'Here N number of hirerchy availabe'>
                ]
            ]
        },

    ]
}

Requirement.要求。

[{
    "id": 2,
    "parent_id": 1,
    "name": "0",
    "is_active": true,
    "position": 1,
    "level": 1,
},
{
    "id": 3,
    "parent_id": 1,
    "name": "01",
    "is_active": false,
    "position": 1,
    "level": 2,
},
{
    "id": 3,
    "parent_id": 1,
    "name": "01",
    "is_active": false,
    "position": 1,
    "level": 2,
},{
    <N Number of dictionary data with consistant sequence>
}]

The suitable answer will definitely acceptable.合适的答案肯定会被接受。

You can flatten the given nested data structure with a recursive function like this:您可以使用如下递归函数展平给定的嵌套数据结构:

def flatten(data):
    if isinstance(data, dict):
        return [data, *flatten(data.pop('children', ()))]
    return [subrecord for record in data for subrecord in flatten(record)]

Demo: https://repl.it/@blhsing/BlankHatefulResources演示: https : //repl.it/@blhsing/BlankHatefulResources

I have found the solution to my question.我找到了我的问题的解决方案。 Below code is working for me.下面的代码对我有用。

if isinstance(categories, dict):
    values = {
        'name': categories.get('name'),
        'parent_id': categories.get('parent_id'),
        'magento_id': categories.get('id'),
        'instance_id': instance.id
    }
    self.category_list.append(values)
    self._analyse_response_data(categories.get('children_data'), instance)
if isinstance(categories, list):
    for category in categories:
        values = {
            'name': category.get('name'),
            'parent_id': category.get('parent_id'),
            'magento_id': category.get('id'),
            'instance_id': instance.id
        }
        self.category_list.append(values)
        self._analyse_response_data(category.get('children_data'), instance)
return self.category_list

I have used recursion to fulfil my requirement.我已经使用递归来满足我的要求。

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

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