in a dictionary whose keys are the warehouses: associated with each warehouse is an inner dictionary whose keys are the stocked products (and whose associated values are the inventory of that product in the warehouse). The inventory must always be a non-negative value; an inventory of 0 is legal. For example, a simple/small database might be.
db = {'Irvine' : {'brush': 3, 'comb': 2, 'wallet': 2},
'Newport': {'comb': 7, 'stapler': 0},
'Tustin' : {'keychain': 3, 'pencil': 4, 'wallet': 3}}
This data structure means that
The Irvine warehouse stocks 3 brushes, 2 combs, and 2 wallets.
The Newport warehouse stocks 7 combs, and 0 staplers.
The Tustin warehouse stocks 3 keychains, 4 pencils, and 3 wallets.
(b) The by_store_inventory function returns a list of str (warehouse names), sorted descending by which warehouses have the largest inventory (summed over all the products). If two warehouses store the same inventory, they should appear in ascending order of warehouse name: for the db dictionary above the result is ['Tustin', 'Irvine', 'Newport'].
OUTPUT :
CODE :
def by_store_inventory(db):
ldb = [(x,sum(y.values())) for x,y in db.items()]
#ldb stores warehouse,total products tuples
#print(ldb)
#We need to sort it based on total products and warehouse
name
ldb.sort(key = lambda x:(-x[1],x[0]))
#print(ldb)
#Return the names
ans = [x[0] for x in ldb]
return ans
#Given Warehouse Inventory list
db = {'Irvine' : {'brush': 3, 'comb': 2, 'wallet': 2}, 'Newport':
{'comb': 7, 'stapler': 0},
'Tustin' : {'keychain': 3, 'pencil': 4, 'wallet': 3}}
print(by_store_inventory(db))
in a dictionary whose keys are the warehouses: associated with each warehouse is an inner dictionary...