Ergo
Ergo

Reputation: 393

Utilising Python dictionaries

I have a program which writes a text file containing names and some queries. The first four lines start by defining a parental figure on left and their children after the colon; think of it as a family tree if you will. The exercise asks us to use dictionaries to help solve this problem.

This is how the file starts off..

test_file = open('relationships.txt', 'w')
test_file.write('''Sue: Chad, Brenda, Harris
Charlotte: Tim
Brenda: Freddy, Alice
Alice: John, Dick, Harry

mother Sue
mother Charlotte
mother Brenda
mother Dick
''')
test_file.close()

The output should be ..

Mother not known
Mother not known
Sue
Alice

I'm unsure on how to create this mother query that checks to see which mother the child belongs to. I've tried a few things such as..

parents = {}

for line in lines[0:4]:
    parent, child = line.strip().split(':')

    if parent in parents:
        parents[parent] += str(child)
    else:
        parents[parent] = str(child)

print(parents)

I'm stuck at this point on how to then access and figure out whose mother is who. The only other way I can think of which is far less elegant would be to switch the key and value around to have a huge list of lines individually labelling every child's mother.

Upvotes: 0

Views: 150

Answers (2)

kampu
kampu

Reputation: 1421

To actually address your question of using dictionaries:

parentchild_map = {}
for line in lines:
    if ':' not in line:
         continue
    mother, multichildren = line.split(':')
    children = multichildren.strip().split(', ')
    parentchild_map[mother] = children

Then you can check for a match like this:

 for parent, children in parentchild_map.items():
     if child in children:
         print ("Mother is ", parent)
         break
 else:
     print ('Mother not known.')

(EDIT: added missing "break" in above code)

To make the lookup faster, you could generate a reverse dictionary ahead of time

 reversemap = {}
 for parent, children in parentchild_map.items():
     for child in children:
         reversemap[child] = parent

then you would be able to just go:

 mother = reversemap.get(child)
 if mother:
     print ('Mother is ', mother)
 else:
     print ('Mother unknown.')

Whichever query algorithm you chose, the first or second one, I expect you would put it in a function accepting a 'child' parameter, so you could easily do as many queries as you want.

Upvotes: 2

Blender
Blender

Reputation: 298196

You should keep a list of children, not a single string:

for line in lines[0:4]:
    parent, child = line.strip().split(':')

    if parent in parents:
        parents[parent].append(child)
    else:
        parents[parent] = [child]

Now, you can iterate over the parents and just check for a particular child:

child = 'Peter'

for parent, children in parents.items():
    if child in children:
        print('Mother is', parent)
        break
else:
    print('Mother not known')

Building a dictionary that maps children to their parents would make the lookup faster.

Upvotes: 3

Related Questions