Python Forum
Names and codes - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Names and codes (/thread-3286.html)



Names and codes - janek30 - May-11-2017

I have a dictionary to people's names and codes,like this:
  Names_and_codes = {"Emily":30623,
                  'Ethan':21298,
                  "Chloe":40145,
                  "Olivia":61834,
                  "Daniel":51398,
                  "William":82523,
                  "Emma":70623}

 
And I have a list of codes,like this :
codes =[[30623 ,70623],
        [61834 ,40145],
        [51398, 21298],
        [21298 ,40145],
        [61834 ,82523],
        [21298, 82523]] 
The first number in list is the parents number and the second children's number...I need to find my parents and their kids,And put them in a new dictionary... like this:
 new_dic = {'Ethan': {'Chloe', 'William'},
'Olivia': {'Chloe', '"William'}, 
'Daniel': {'Ethan'}, 
'Emily': {'Emma'}}

 



RE: Names and codes - Larz60+ - May-11-2017

So if you show what you've done so far, in addition to entering the lists,
I'll (or any of the other moderators) will be glad to help finish it


RE: Names and codes - janek30 - May-11-2017

Actually, it's a little longer homework...,First, I had to get the data from the file, I got it done......
CODE:
   
    def Children_and_parents(f1, f2):
    f = open(f2, encoding = "UTF-8")
    ff = open(f1, encoding = "UTF-8") 
    m = {}
    s = []
    new_dic = "?????"
    for i in f:
        d = i.split()
        for osa in d:
            m[" ".join(d[1:])]= d[0]
            for ri in ff:
                os = ri.split()
                s.append(os)
    return(new_dic)
But now I'm stuck, How i find parents and their children,and put them in a new dictionary(new_dic)


RE: Names and codes - nilamo - May-11-2017

Start with a function that takes the parent's name/code, and returns that parent's children. That should be a small stepping stone to helping you solve this.