Q:

Python code to find all persons based on qty of books sold

Hi, i got stuck with the below code, while I was trying to find solution for the question:
Find all persons based on quantity of books sold.
below is the content of the file 1711.txt

david book 05 walmart
emily book 10 kroger
jason food 11 target
david food 20 kroger
eva cloth 13 target
david book 5 walmart

the output should be something like : david sold 10 books to walmart

eva sold 13 cloths to target

david sold 20 food to kroger

below is the code that I wrote, please assist me to with the correct code to display the correct result,
Thanks!
d={}
p={}
q={}

with open("1711.txt","r") as f:
    for line in f:
        a=line.split()
        l,m,n,o=str(a[0]), str(a[1]), int(a[2]), str(a[3])
        d.setdefault(l,[]).append(n)
        p.setdefault(m,[]).append(n)
        q.setdefault(o,[]).append(n)

    d=dict((key,sum(val)) for key,val in d.items())
    print (d)
    print("----------------------")
    p=dict((keys,sum(values)) for keys,values in p.items())
    print (p)
    print("--------------------")

    q=dict((keeys,sum(valu)) for keeys,valu in q.items())
    print (q)
    print("----------------------------")

    for k, v in d.items():
        print("number of itmes sold by {} is {}".format(k,v))
    print("------------------------------")

    for k, v in q.items():
        print("number of itmes in {} is {}".format(k,v))
    print("------------------------------")

    for k, v in p.items():
        print("total number of {} sold is {}".format(k,v))
    print("------------------------------")

A:

Sounds like you want some nested dictionaries. The top level one should have keys that are all the names, and the values should be another dictionary.
Those dictionaries should have keys of the items, and the value should be another dictionary.
Finally, those dictionaries should have keys that have the store, and the values should be the number that have been sold to that store.
So then if you go: sales_dict["david"]["book"]["target"] it will spit out 10 for you.
edit: 
Here's the gist of creating the dictionary. I think you should probably refer to https://docs.python.org/3/tutorial/datastructures.html#dictionaries if you are still having issues.
d = {}
for line in f:
      a=line.split()
      l,m,n,o=str(a[0]), str(a[1]), int(a[2]), str(a[3])
      if l not in d:
            d[l] = {}
      if m not in d[l]:
            d[l][m] = {}
      if n not in d[l][m]:
            d[l][m][n] = 0
      d[l][m][n] += int(o)

