Home > Blockchain >  How to sort dictionary by value from list using Python
How to sort dictionary by value from list using Python

Time:10-05

I have data of dictionary:

dict_source = {
    'K93': [(('QB2MRR-GASY93RD01', 'ASSYMIRROR 2W SHIFT 2 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 1, 'K93', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]), 
            (('QB2MRR-GASY93RD01', 'ASSYMIRROR 2W SHIFT 3 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 2, 'K93', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),], 
    'K0JA': [(('QB2MRR-GASY0JBK01', 'ASSYMIRROR 2W SHIFT 2 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 1, 'K0JA', 'AMK0JA  ', 0.0, 700.0, 400), [600, 30]), 
            (('QB2MRR-GASY0JBK01', 'ASSYMIRROR 2W SHIFT 3 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 3, 'K0JA', 'AMK0JA  ', 0.0, 700.0, 400), [300, 15])], 
    'K03S': [(('QB2MRR-GANT3SBK01', 'ASSYMIRROR 2W SHIFT 3 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 1, 'K03S', 'AMK03S  ', 0.0, 900.0, 600), [600, 30]), 
            (('QB2MRR-GANT3SBK01', 'ASSYMIRROR 2W SHIFT 3 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 2, 'K03S', 'AMK03S  ', 0.0, 900.0, 600), [300, 15])], 
    'K1AA': [(('QB2MRR-GASY1ABK01', 'ASSYMIRROR 2W SHIFT 2 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 2, 'K1AA', 'AMK1AA  ', 0.0, 900.0, 600), [200.0, 10]), 
            (('QB2MRR-GASY1ABK01', 'ASSYMIRROR 2W SHIFT 3 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 1, 'K1AA', 'AMK1AA  ', 0.0, 900.0, 600), [600, 30])]
}

and i have a collection of product type sequences in the form of a list

my_list_type_product = ["K03S", "K1AA", "K93", "K0JA"]

and my question is how to sort my dictionary based on my_list_type_product?. The result what i want like this:

{
    'K03S': [(('QB2MRR-GANT3SBK01', 'ASSYMIRROR 2W SHIFT 3 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 1, 'K03S', 'AMK03S  ', 0.0, 900.0, 600), [600, 30]), 
            (('QB2MRR-GANT3SBK01', 'ASSYMIRROR 2W SHIFT 3 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 2, 'K03S', 'AMK03S  ', 0.0, 900.0, 600), [300, 15])], 
    'K1AA': [(('QB2MRR-GASY1ABK01', 'ASSYMIRROR 2W SHIFT 2 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 2, 'K1AA', 'AMK1AA  ', 0.0, 900.0, 600), [200.0, 10]), 
            (('QB2MRR-GASY1ABK01', 'ASSYMIRROR 2W SHIFT 3 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 1, 'K1AA', 'AMK1AA  ', 0.0, 900.0, 600), [600, 30])],
    'K93': [(('QB2MRR-GASY93RD01', 'ASSYMIRROR 2W SHIFT 2 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 1, 'K93', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]), 
            (('QB2MRR-GASY93RD01', 'ASSYMIRROR 2W SHIFT 3 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 2, 'K93', 'AMK2FJSRM', 0.0, 600.0, 300), [300, 15]),], 
    'K0JA': [(('QB2MRR-GASY0JBK01', 'ASSYMIRROR 2W SHIFT 2 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 1, 'K0JA', 'AMK0JA  ', 0.0, 700.0, 400), [600, 30]), 
            (('QB2MRR-GASY0JBK01', 'ASSYMIRROR 2W SHIFT 3 Daily', 'ASSYMIRROR 2W LINE 1', 1200, 3, 'K0JA', 'AMK0JA  ', 0.0, 700.0, 400), [300, 15])]
}

CodePudding user response:

Suggest using panda dataframe:

d = pd.DataFrame(dict_source).transpose().sort_index().to_dict()
sortedDict = {list(v.keys())[0]: list(v.values())[0] for k,v in d.items()}

CodePudding user response:

If you get the keys and just order them and then pull the original dictionary's property values in, it becomes simpler to handle.

ordered_source = { key: dict_source[key] for key in sorted(dict_source.keys()) }
  • Related