Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Names and codes
#1
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'}}

 
Reply
#2
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
Reply
#3
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)
Reply
#4
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020