I would like to create a product category representation as a python dictionary. The input is the following list of product categories, represented as list of lists:
categories_list = [
['computer', 'laptop'],
['computer', 'desktop'],
['computer', 'processor', 'Intel'],
['computer', 'processor', 'AMD'],
['computer', 'accessories', 'mouse'],
['computer', 'accessories', 'keyboard'],
['controller', 'raspberryPI'],
['controller', 'Arduino'],
['controller', 'accessories'],
['other'],
['electronics', 'other']
]
I would like to turn the list into a dictionary like this:
categories_dict = {
'computer': {'laptop': None,
'desktop': None,
'processor': {'Intel': None,
'AMD': None},
'accessories': {'mouse': None,
'keyboard': None}},
'controller': {'raspberryPI': None,
'Arduino': None,
'accessories': None},
'other': None,
'electronics': {'other': None}
}
This represents the hierarchy. The main levels are: computer, controller, other, electronics.
Some of these have children, and some of those children have also their children.
For example the computers children are:
laptop, desktop, processor, accessories
-the processor and accessories have also subchildren:
Intel, AMD - for processor
mouse, keyboard - for accessories
None indicates that there are no further children/subcategories.
My code so far creates some tree structure, but this is still not what I need.
def create_cat_dict():
dict_tree = {}
main_name = ""
for cat_list in all_categories:
for index, cat_name in enumerate(cat_list):
if index == 0 and cat_name not in dict_tree:
main_name = cat_name
dict_tree[cat_name] = {}
if index > 0 and cat_name not in dict_tree[main_name].keys():
dict_tree[main_name][cat_name] = None
print(dict_tree)
It creates this dictionary, which is still not correct:
{'computer': {'AMD': None,
'Intel': None,
'accessories': None,
'desktop': None,
'keyboard': None,
'laptop': None,
'mouse': None,
'processor': None},
'controller': {'Arduino': None,
'accessories': None,
'raspberryPI': None},
'electronics': {'other': None},
'other': {}}
Under computer>processor should be {'Intel': None, 'AMD': None}
Under computer>accessories should be {'mouse': None, 'keyboard': None}
I would like to ask for help on how to read all the categories, even if a category level is 4, 5, 6 level deep.
CodePudding user response:
Here is a solution that creates a dictionary representing the hierarchy of product categories from the given list of categories:
def create_cat_dict(categories_list):
# Initialize an empty dictionary
categories_dict = {}
# Iterate over the list of categories
for cat_list in categories_list:
# Initialize a reference to the current level in the dictionary
current_level = categories_dict
# Iterate over the items in the category list
for cat_name in cat_list:
# If the category name is not in the current level, add it
if cat_name not in current_level:
current_level[cat_name] = {}
# Update the reference to the current level
current_level = current_level[cat_name]
# Return the dictionary representing the hierarchy of product categories
return categories_dict
# Example
categories_list = [
['computer', 'laptop'],
['computer', 'desktop'],
['computer', 'processor', 'Intel'],
['computer', 'processor', 'AMD'],
['computer', 'accessories', 'mouse'],
['computer', 'accessories', 'keyboard'],
['controller', 'raspberryPI'],
['controller', 'Arduino'],
['controller', 'accessories'],
['other'],
['electronics', 'other']
]
categories_dict = create_cat_dict(categories_list)
print(categories_dict)
The output will be a dictionary representing the hierarchy of product categories:
{'computer': {'laptop': None,
'desktop': None,
'processor': {'Intel': None, 'AMD': None},
'accessories': {'mouse': None, 'keyboard': None}},
'controller': {'raspberryPI': None, 'Arduino': None, 'accessories': None},
'other': None,
'electronics': {'other': None}}
This solution works by iterating over the list of categories and adding each category to the dictionary at the appropriate level, creating new levels as needed. It updates a reference to the current level in the dictionary as it iterates over the items in the category list, and adds each item to the current level if it does not already exist. At the end, it returns the dictionary representing the hierarchy of product categories.
CodePudding user response:
One way using dict.setdefault
:
def nest(d, keys, value=None):
for k in keys[:-1]:
d = d.setdefault(k, {})
d[keys[-1]] = value
res = {}
for l in categories_list:
nest(res, l)
Output:
{'computer': {'accessories': {'keyboard': None, 'mouse': None},
'desktop': None,
'laptop': None,
'processor': {'AMD': None, 'Intel': None}},
'controller': {'Arduino': None, 'accessories': None, 'raspberryPI': None},
'electronics': {'other': None},
'other': None}