without haste but without rest

[python / 계프] 5주차 과제 - 딕셔너리 본문

Homework

[python / 계프] 5주차 과제 - 딕셔너리

JinungKim 2020. 4. 17. 18:01
/*
input data

anne 1 3 6 4 5 6 3 5
john 3 2 3 5 7 2 
peter 9 8 2 7 4 9 
end 


*/


def print_nested_dict(my_dict):
    for x in my_dict:
        if my_dict[x] == 1:
            print(x, end=' ')
        else:
            print('{}({})'.format(x, my_dict[x]), end=' ')
    print()


def print_dict(my_dict):
    for x in my_dict:
        print(x, ':', end=' ', sep='')
        if type(my_dict[x]) == dict:
            print_nested_dict(my_dict[x])
        else:
            print(my_dict[x])


def list_to_dict(nlist):
    n_dict = {}
    for a in nlist:
        if a in n_dict:
            n_dict[a] += 1
        else:
            n_dict[a] = 1
    return n_dict


orders = {}
nested_orders = {}
items = {}

while True:
    tmp = input().split()
    name = tmp[0]
    if name == 'end':
        break
    order_list = list(map(int, tmp[1:]))
    orders[name] = order_list
    nested_orders[name] = list_to_dict(order_list)

for a in nested_orders:
    for b in nested_orders[a]:  # *a's values(dict type)
        if b not in items:
            items[b] = {}
        items[b][a] = nested_orders[a][b]

print_dict(orders); print()
print_dict(nested_orders); print()
print_dict(items)
Comments