aisyah kumala dewi aisyah kumala dewi - 2 months ago 6
Python Question

Transform values of a dictionary to new dictionary

I have a dictionary:

{
'doc0': {
'individu': 1,
'manajemen': 1,
'tahu': 1,
'logistik': 1,
'transaksi': 1
},
'doc1': {
'manajemen': 1,
'transfer': 1,
'individu':1,
'tahu':1,
'transaksi': 1,
'logistik': 1
},
'doc2': {
'manajemen': 1,
'logistik': 1,
'transaksi': 1
}
}


I want use Python to transform it to the following in a new dictionary:

{
'doc0': {
'individu': 1,
'manajemen': 1,
'tahu': 1,
'logistik': 1,
'transaksi': 1,
'transfer':0
},
'doc1': {
'individu': 1,
'manajemen': 1,
'tahu': 1,
'logistik': 1,
'transaksi': 1
},
'doc2': {
'individu': 0,
'manajemen': 1,
'tahu': 0,
'logistik': 1,
'transaksi': 1,
'transfer':0
}
}

Answer

You will face the problem with the key ordering in a dictionary. The keys (or the key-value pairs) in the dictionary are sorted arbitrarily. The order is not fixed and can change in different steps.

To mitigate this problem you can use the OrderedDict from the module collections.

If I understand your problem, you want to bring in line your dictionaries and make them have the same keys. If there is a key in doc1, but doc2 is missing it, so doc2 should be updated with this key and the value 0. If that's right, then you rather want to duplicate the keys, not the values.

This is my try:

from collections import OrderedDict

# your initial data
my_dict = {
    'doc0': {
        'individu': 1,
        'manajemen': 1,
        'tahu': 1,
        'logistik': 1,
        'transaksi': 1
    },
    'doc1': {
        'manajemen': 1,
        'transfer': 1,
        'individu':1,
        'tahu':1,
        'transaksi': 1,
        'logistik': 1
    },
    'doc2': {
        'manajemen': 1,
        'logistik': 1,
        'transaksi': 1
    }
}

# get all keys in a help list
list_of_keys = []
for key in my_dict:
    for dockey in my_dict[key]:
        if dockey not in list_of_keys:
            list_of_keys.append(dockey)
# sort the list
list_of_keys.sort()
# the list looks like this
# ['individu', 'logistik', 'manajemen', 'tahu', 'transaksi', 'transfer']

# now we can iterate through our data
# and align the keys and values in an OrderedDict

my_ordered_dict = OrderedDict()
# get the doc keys and update the ordered dictionary
for key in my_dict:
    my_ordered_dict.update([(key, OrderedDict())])

for i in list_of_keys:
    for key in my_dict:
        if i in my_dict[key].iterkeys():
            my_ordered_dict[key].update([(i, 1)])
        else:
            my_ordered_dict[key].update([(i, 0)])

Now you have all keys in every dictionary, they are sorted and the order is preserved. The initial data stays unchanged.

I didn't use any advanced features like list comprehensions, ternary operators or similar. The approach is a little bit naive and simple because you are a beginner, and I think it would be easier for you to understand every step.

I hope it can help you.