python - splitting list to dictionary -


i have following list object(it's not dictionary yet). list contains name , products , quantity carry huge put 3 samples:

file = open('.txt') rdr = file.read() prod_lists = rdr.split('\n') 

so prod_lists follows:

prod_lists = {'allen':{'tv':100,'playstation':200,'xbox':30}, 'balden':{'tv':200,'laptop':300}, 'cathy':{'watch':100,'printer':200}} 

how split above list (key, value) dictionary? also, how find sum of products each individual. here's tried not working:

prod_dict = dict((k.split(),v.split()) k,v in (i.split('{')for in prod_lists)) key,value in prod_dict.items():     print(key,value) 

i not sure asking outer container dictionary

>>> prod_lists = {'allen':{'tv':100,'playstation':200,'xbox':30}, 'balden':{'tv':200,'laptop':300}, 'cathy':{'watch':100,'printer':200}} >>> type(prod_lists) <class 'dict'> >>> type(prod_lists['allen']) <class 'dict'> 

you can see copied prod_lists , asked python was.

so if want access individually of inner dictionaries need call name

>>> prod_lists['allen'] {'tv': 100, 'playstation': 200, 'xbox': 30} 

the way tell dictionary was enclosed in {}. python introspective , if not sure of type of object have ask

for vendor in prod_lists:     total = 0     prod, q in prod_lists[vendor].items():         print vendor, prod,q         total +=q     print 'vendor total', vendor, total  balden tv 200 balden laptop 300 vendor total balden 500 allen tv 100 allen playstation 200 allen xbox 30 vendor total allen 330 cathy printer 200 cathy watch 100 vendor total cathy 300 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -