-3

I have the following list object(it's not a dictionary yet). The list contains a name and the products and the quantity they carry which is huge but I only put 3 samples:

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

so the prod_lists are as follows:

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

How would I split the above list into a (key, value) dictionary? also, how would I find sum of products for each individual. Here's what I tried to do which is not working:

prod_dict = dict((k.split(),v.split()) for k,v in (i.split('{')for i in prod_lists))
for key,value in prod_dict.items():
    print(key,value)
user3399326
  • 863
  • 4
  • 13
  • 24
  • `prod_lists` is **not** a list, it is a dictionary. – Martijn Pieters Mar 24 '14 at 16:18
  • If you do `type(prod_lists).__name__` you will find that it outputs `dict` as in a **dictionary**, not a `list`. – anon582847382 Mar 24 '14 at 16:19
  • @AlexThornton: the `__name__` part is not necessary, is it.. – Martijn Pieters Mar 24 '14 at 16:19
  • What you have already is a dictionary and not a list. – Alagappan Ramu Mar 24 '14 at 16:19
  • In the answer below, you posted a comment mentioning that prod lists comes from a text file. Does that text file contain the brackets, colons, etc? – mdadm Mar 24 '14 at 16:22
  • Possible duplicate of http://stackoverflow.com/questions/988228/converting-a-string-to-dictionary @user3399326 – Alagappan Ramu Mar 24 '14 at 16:23
  • Can you post the raw contents of the text file you are trying to load please? If it is a large file, just one line. If the line is too long, just truncate it to include at least the beginning and end characters. – mdadm Mar 24 '14 at 16:40
  • 1
    When reading files, consider the safer approach of using file.readlines() instead of splitting based on the occurrence of new lines. It will help to make your code much more portable ('\r\n' in Windows) as well as other benefits. – Christian Groleau Mar 24 '14 at 16:44

1 Answers1

-1

I am not sure what you are asking as the outer container is a 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 I copied your prod_lists and then asked Python what is was.

So if you want to access individually any of the inner dictionaries you just need to call it by name

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

The way I could tell it was a dictionary was that it was enclosed in {}. Python is very introspective and if you are not sure of the type of an object you just have to ask

for vendor in prod_lists:
    total = 0
    for 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
PyNEwbie
  • 4,882
  • 4
  • 38
  • 86