I have the following input data which needs to be stored in the appropriate data structure.
Electronics
Electronics > Arcade Equipment
Electronics > Arcade Equipment > Basketball Arcade Games
Electronics > Arcade Equipment > Video Game Arcade Cabinets
Electronics > Audio
Electronics > Audio > Audio Accessories
Electronics > Audio > Audio Accessories > Audio & Video Receiver Accessories
Electronics > Audio > Audio Accessories > Headphone & Headset Accessories
Electronics > Audio > Audio Accessories > Headphone & Headset Accessories > Headphone Cushions & Tips
Apparel & Accessories > Clothing > Activewear > Hunting Clothing > Hunting & Tactical Pants
Apparel & Accessories > Clothing > Activewear > Martial Arts Shorts
Apparel & Accessories > Clothing > Activewear > Motorcycle Protective Clothing
Apparel & Accessories > Clothing > Baby & Toddler Clothing
Animals & Pet Supplies > Pet Supplies > Pet Grooming Supplies > Pet Nail Polish
Animals & Pet Supplies > Pet Supplies > Pet Grooming Supplies > Pet Nail Tools
Animals & Pet Supplies > Pet Supplies > Pet Grooming Supplies > Pet Shampoo & Conditioner
Arts & Entertainment > Hobbies & Creative Arts > Arts & Crafts > Art & Crafting Tools > Cutting Mats
Arts & Entertainment > Hobbies & Creative Arts > Arts & Crafts > Art & Crafting Tools > Frames, Hoops & Stretchers
Arts & Entertainment > Hobbies & Creative Arts > Arts & Crafts > Art & Crafting Tools > Glue Guns
Arts & Entertainment > Hobbies & Creative Arts > Arts & Crafts > Art & Crafting Tools > Light Boxes
Which should give following infomation.
'Animals & Pet Supplies': 3,
'Animals & Pet Supplies > Pet Supplies': 3,
'Animals & Pet Supplies > Pet Supplies > Pet Grooming Supplies': 3,
'Animals & Pet Supplies > Pet Supplies > Pet Grooming Supplies > Pet Nail Polish': 1,
'Animals & Pet Supplies > Pet Supplies > Pet Grooming Supplies > Pet Nail Tools': 1,
'Animals & Pet Supplies > Pet Supplies > Pet Grooming Supplies > Pet Shampoo & Conditioner': 1,
'Apparel & Accessories': 4,
'Apparel & Accessories > Clothing': 4,
'Apparel & Accessories > Clothing > Activewear': 3,
'Apparel & Accessories > Clothing > Activewear > Hunting Clothing': 1,
'Apparel & Accessories > Clothing > Activewear > Hunting Clothing > Hunting & Tactical Pants': 1,
'Apparel & Accessories > Clothing > Activewear > Martial Arts Shorts': 1,
'Apparel & Accessories > Clothing > Activewear > Motorcycle Protective Clothing': 1,
'Apparel & Accessories > Clothing > Baby & Toddler Clothing': 1,
'Arts & Entertainment': 4,
'Arts & Entertainment > Hobbies & Creative Arts': 4,
'Arts & Entertainment > Hobbies & Creative Arts > Arts & Crafts': 4,
'Arts & Entertainment > Hobbies & Creative Arts > Arts & Crafts > Art & Crafting Tools': 4,
'Arts & Entertainment > Hobbies & Creative Arts > Arts & Crafts > Art & Crafting Tools > Cutting Mats': 1,
'Arts & Entertainment > Hobbies & Creative Arts > Arts & Crafts > Art & Crafting Tools > Frames, Hoops & Stretchers': 1,
'Arts & Entertainment > Hobbies & Creative Arts > Arts & Crafts > Art & Crafting Tools > Glue Guns': 1,
'Arts & Entertainment > Hobbies & Creative Arts > Arts & Crafts > Art & Crafting Tools > Light Boxes': 1,
'Electronics': 9,
'Electronics > Arcade Equipment': 3,
'Electronics > Arcade Equipment > Basketball Arcade Games': 1
I have implemented using python3 in following way which has time complexity of O(n) where n is number of lines in file. Is there other efficient solution to store above information.
def get_data(file_path):
try:
if not os.path.exists(file_path): # check file exist or not
raise FileNotFoundError
except FileNotFoundError:
print('please provide valid file path')
exit()
inventory = {}
with open(file=file_path, mode='r', encoding='utf-8') as file_descr:
try:
for line in file_descr:
line = line.strip()
llist = line.split('>') # list of keys
for i in range(1, len(llist) + 1): # check entry of each possible key in inventory
key = ('>'.join(llist[:i])).strip()
if inventory.get(key):
inventory[key] += 1
else:
inventory[key] = 1
except Exception as e:
print('not able to read content', e)
exit()
return inventory