英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论