Home > Mobile >  How to print a nested defaultdict without including the class/type?
How to print a nested defaultdict without including the class/type?

Time:10-08

I have the following code:

from collections import defaultdict
db1 = {'Adam': {'Cleaning': 4, 'Tutoring': 2, 'Baking': 1},
        'Betty': {'Gardening': 2, 'Tutoring': 1, 'Cleaning': 3},
        'Charles': {'Plumbing': 2, 'Cleaning': 5},
        'Diane': {'Laundry': 2, 'Cleaning': 4, 'Gardening': 3}}

def by_skill(db1 : {str:{str:int}}) -> [int,[str,[str]]]:
    order_skills = defaultdict(lambda:defaultdict(list))
    for k,v in db1.items():
        for key,value in v.items():
            order_skills[value][key].append(k)

    dict(order_skills)
    order_skills_sorted = sorted( sorted(order_skills.items()), reverse=True )
    return order_skills_sorted


if __name__ == '__main__':
    print(by_skill(db1))

With the output:

[(5, defaultdict(<class 'list'>, {'Cleaning': ['Charles']})), (4, defaultdict(<class 'list'>, {'Cleaning': ['Adam', 'Diane']})), (3, defaultdict(<class 'list'>, {'Cleaning': ['Betty'], 'Gardening': ['Diane']})), (2, defaultdict(<class 'list'>, {'Tutoring': ['Adam'], 'Gardening': ['Betty'], 'Plumbing': ['Charles'], 'Laundry': ['Diane']})), (1, defaultdict(<class 'list'>, {'Baking': ['Adam'], 'Tutoring': ['Betty']}))]

But I need the output to be (formatted for readability, not a requirement): (in alphabetical order)

[(5, [('Cleaning', ['Charles'])]),
(4, [('Cleaning', ['Adam', 'Diane'])]),
(3, [('Cleaning', ['Betty']), ('Gardening', ['Diane'])]),
(2, [('Gardening', ['Betty']), ('Laundry', ['Diane']),
 ('Plumbing', ['Charles']), ('Tutoring', ['Adam'])]),
(1, [('Baking', ['Adam']), ('Tutoring', ['Betty'])])]

Would I have to call sorted a 3rd time to achieve this?

CodePudding user response:

Since the ratings are ranged between 1 and 5, you can build a rating-keyed dict of skill-keyed dicts of lists of names and then iterate through the ratings to extract the names by skills in linear time:

by_skill = {}
for name, skills in db1.items():
    for skill, rating in skills.items():
        by_skill.setdefault(rating, {}).setdefault(skill, []).append(name)
print([
    (
        rating,
        sorted(
            (skill, sorted(names)) for skill, names in by_skill.get(rating, {}).items()
        )
    )
    for rating in range(5, 0, -1)
])

This outputs:

[(5, [('Cleaning', ['Charles'])]), (4, [('Cleaning', ['Adam', 'Diane'])]), (3, [('Cleaning', ['Betty']), ('Gardening', ['Diane'])]), (2, [('Gardening', ['Betty']), ('Laundry', ['Diane']), ('Plumbing', ['Charles']), ('Tutoring', ['Adam'])]), (1, [('Baking', ['Adam']), ('Tutoring', ['Betty'])])]
  • Related