Python cheat sheet
Jump to navigation
Jump to search
Two dimension dictionary with embedded arrays
#Init Dict dict = {} #Inventory for Category Fruits dict['1000A'] = {} dict['1000A']['Category'] = 'Fruits' dict['1000A']['Items'] = 'Orange, Lemmon, Apple' dict['1000A']['Location'] = 'New York' #Inventory for Category Cars dict['1000B'] = {} dict['1000B']['Category'] = 'Cars' dict['1000B']['Items'] = 'Audi, BMW, Mercedes, VW' dict['1000B']['Location'] = 'London' #Inventory for Category Bikes dict['1000C'] = {} dict['1000C']['Category'] = 'Bikes' dict['1000C']['Items'] = 'Ducati, KTM, Yamaha, Husquarna' dict['1000C']['Location'] = 'Frankfurt' #List the dict inventory for pnt in dict: print("-ID", pnt) print("\t-Category", dict[pnt]['Category']) print("\t-Location", dict[pnt]['Location']) print("\t-Items:") #Split the dict[pnt]['Items'] string, then strip and map into the set named items items = map(str.strip, dict[pnt]['Items'].split(',')) #items contains now a set of dict[pnt]['Items'] for item in items: print("\t\t-", item)
Output
-ID 1000A -Category Fruits -Location New York -Items: - Orange - Lemmon - Apple -ID 1000B -Category Cars -Location London -Items: - Audi - BMW - Mercedes - VW -ID 1000C -Category Bikes -Location Frankfurt -Items: - Ducati - KTM - Yamaha - Husquarna