Skip to content Skip to sidebar Skip to footer

Creating Dictionaries To List Order Of Ranking

I have a list of people and who controls who but I need to combine them all and form several sentences to compute which person control a list of people. The employee order comes fr

Solution 1:

from collections import defaultdict

controls = defaultdict(list)

with open('data.txt') as file:
    for line in file:
        controller, controlled = line.strip().split(' controls ')
        controls[controller].append(controlled)

print 'employee order:'
for controller, controlled in controls.iteritems():       
    if len(controlled) > 1:
        conjoined = ', '.join(controlled[:-1])
        conjoined = '{} and {}'.format(conjoined, controlled[-1])
    else:
        conjoined = controlled[0]
    print '{} controls {}'.format(controller, conjoined)

Post a Comment for "Creating Dictionaries To List Order Of Ranking"