如何将类似项分组成一个Python字典列表以用于HTML ul列表

huangapple go评论71阅读模式
英文:

How to group similar items into a python list of dictionaries for HTML ul list

问题

我正在尝试创建一个HTML选择菜单。我有以下Python数据结构:

categories = [
    ['Sports & Outdoors', 'Outdoor Recreation', 'Skates, Skateboards & Scooters'],
    ['Toys & Games', 'Learning & Education', 'Science Kits & Toys'],
    ['Toys & Games', 'Arts & Crafts', 'Craft Kits'],
    ['Toys & Games', 'Arts & Crafts', 'Drawing & Painting Supplies', 'Crayons'],
    ['Home & Kitchen', 'Home Décor', 'Window Treatments', 'Window Stickers & Films']
]

然而,为了使其与HTML/JQuery菜单配合工作,我需要将上面的数据结构更改为以下结构:

categories = [
    {
        "name": 'Sports & Outdoors',
        "subcategory": [
            {
                "name": 'Outdoor Recreation',
                "subcategory": [
                    {
                        "name": 'Skates, Skateboards & Scooters',
                        "last": True
                    }
                ]
            }
        ]
    },
    {
        "name": 'Toys & Games',
        "subcategory": [
            {
                "name": 'Learning & Education',
                "subcategory": [
                    {
                        "name": 'Science Kits & Toys',
                        "last": True
                    }
                ]
            },
            {
                "name": 'Arts & Crafts',
                "subcategory": [
                    {
                        "name": 'Craft Kits',
                        "last": True
                    },
                    {
                        "name": 'Drawing & Painting Supplies',
                        "subcategory": [
                            {
                                "name": 'Crayons',
                                "last": True
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "name": 'Home & Kitchen',
        "subcategory": [
            {
                "name": 'Home Décor',
                "subcategory": [
                    {
                        "name": 'Window Treatments',
                        "subcategory": [
                            {
                                "name": 'Window Stickers & Films',
                                "last": True
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

我无法理解它。任何帮助将不胜感激。如何将Python的嵌套列表转换为上述的字典列表,并同时进行分组?如果可能,在树的末端标明"last": True

英文:

I am trying to create an HTML selected menu. I have the following python data structure:

categories = [
        ['Sports & Outdoors', 'Outdoor Recreation', 'Skates, Skateboards & Scooters']
        ['Toys & Games', 'Learning & Education', 'Science Kits & Toys']
        ['Toys & Games', 'Arts & Crafts', 'Craft Kits']
        ['Toys & Games', 'Arts & Crafts', 'Drawing & Painting Supplies', 'Crayons']
        ['Home & Kitchen', 'Home Décor', 'Window Treatments', 'Window Stickers & Films']
    ]

However to get this to work with the HTML/JQuery menu, I need to change the above data structure to this:

categories = [
            {
                "name": 'Sports & Outdoors',
                "subcategory": [
                    {
                        "name": 'Outdoor Recreation',
                        "subcategory": [
                            {
                                "name": 'Skates, Skateboards & Scooters',
                                "last": True
                            }
                        ]
                    }
                ]
            },
            {
            "name": 'Toys & Games',
            "subcategory": [
                {
                    "name": 'Learning & Education',
                    "subcategory": [
                        {
                        "name": 'Science Kits & Toys',
                        "last": True
                        }
                    ]
                },
                {
                    "name": 'Arts & Crafts',
                    "subcategory": [
                        {
                            "name": 'Craft Kits',
                            "last": True
                        },
                        {
                            "name": 'Drawing & Painting Supplies',
                            "subcategory": [
                                {
                                    "name": 'Crayons',
                                    "last": True
                                }
                            ]
                        }
                    ]
                },
                
            ]
                
            },
            {
                "name": 'Home & Kitchen',
                "subcategory": [
                    {
                    "name": 'Home Décor',
                        "subcategory": [
                            {
                                "name": 'Window Treatments',
                                "subcategory": [
                                    {
                                        "name": 'Window Stickers & Films',
                                        "last": True
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]

I can't get to wrap my head around it. Any help is appreciated. How can I convert the python list of list, to a list of dictionary items as shown above, at the same time doing the grouping? If possible at the end of the tree, indicating with a "last": True.

答案1

得分: 1

请尝试以下代码:

categories = [
    ['Sports & Outdoors', 'Outdoor Recreation', 'Skates, Skateboards & Scooters'],
    ['Toys & Games', 'Learning & Education', 'Science Kits & Toys'],
    ['Toys & Games', 'Arts & Crafts', 'Craft Kits'],
    ['Toys & Games', 'Arts & Crafts', 'Drawing & Painting Supplies', 'Crayons'],
    ['Home & Kitchen', 'Home Decor', 'Window Treatments', 'Window Stickers & Films']
]

new_categories = []
for category in categories:
    existing_category = next((c for c in new_categories if c["name"] == category[0]), None)
    if existing_category:
        new_category = existing_category
    else:
        new_category = {"name": category[0], "subcategory": []}
        new_categories.append(new_category)
    
    last_subcategory = new_category
    for subcategory in category[1:]:
        existing_subcategory = next((c for c in last_subcategory["subcategory"] if c["name"] == subcategory), None)
        if existing_subcategory:
            new_subcategory = existing_subcategory
        else:
            new_subcategory = {"name": subcategory, "subcategory": []}
            last_subcategory["subcategory"].append(new_subcategory)
        last_subcategory = new_subcategory
    last_subcategory["last"] = True

print(new_categories)

希望这能帮助你!

英文:

try this:

categories = [
        ['Sports & Outdoors', 'Outdoor Recreation', 'Skates, Skateboards & Scooters'],
        ['Toys & Games', 'Learning & Education', 'Science Kits & Toys'],
        ['Toys & Games', 'Arts & Crafts', 'Craft Kits'],
        ['Toys & Games', 'Arts & Crafts', 'Drawing & Painting Supplies', 'Crayons'],
        ['Home & Kitchen', 'Home Decor', 'Window Treatments', 'Window Stickers & Films']
    ]

new_categories = []
for category in categories:
    existing_category = next((c for c in new_categories if c["name"] == category[0]), None)
    if existing_category:
        new_category = existing_category
    else:
        new_category = {"name": category[0], "subcategory": []}
        new_categories.append(new_category)
    
    last_subcategory = new_category
    for subcategory in category[1:]:
        existing_subcategory = next((c for c in last_subcategory["subcategory"] if c["name"] == subcategory), None)
        if existing_subcategory:
            new_subcategory = existing_subcategory
        else:
            new_subcategory = {"name": subcategory, "subcategory": []}
            last_subcategory["subcategory"].append(new_subcategory)
        last_subcategory = new_subcategory
    last_subcategory["last"] = True


print(new_categories)

huangapple
  • 本文由 发表于 2023年1月9日 00:24:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定