python - Convert list of lists to custom dictionary -
i'm unsuccessfully trying convert list of lists custom dictionary. i've created following output saved in 2 lists:
headers = ['cpu', 'name', 'id', 'cused', 'callc', 'mused', 'mallc'] result = [['1/0', 'aaa', '10', '0.1', '15', '10.73', '16.00'], ['1/0', 'bbb', '10', '0.1', '20', '11.27', '14.00'], ['1/0', 'ccc', '10', '0.2', '10', '11.50', '15.00'], ['1/0', 'aaa', '10', '1.1', '15', '15.10', '23.00']] formatted output: headers: slot name id cused callc mused mallc result: 1/0 aaa 10 0.1 15 10.73 16.00 2/0 bbb 25 0.1 20 11.39 14.00 1/0 ccc 10 0.2 10 11.50 15.00 1/0 aaa 10 1.1 15 15.10 23.00 the first n columns (3 in case) should used concatenate key name of remaining columns output values. convert dictionary in following format:
slot.<slot>.name.<name>.id.<id>.cused:<value>, slot.<slot>.name.<name>.id.<id>.callc:<value>, slot.<slot>.name.<name>.id.<id>.mused:<value>, slot.<slot>.name.<name>.id.<id>.mallc:<value>, ... for example:
dictionary = { 'slot.1/0.name.aaa.id.10.cused':10, 'slot.1/0.name.aaa.id.25.callc':15, 'slot.1/0.name.aaa.id.10.mused':10.73, 'slot.1/0.name.aaa.id.10.mallc':16.00, 'slot.2/0.name.bbb.id.10.cused':0.1, ... 'slot.<n>.name.<name>.id.<id>.<value_name> <value> } can show me how can done?
try this, note: changed "slot" instead of "cpu"
headers = ['slot', 'name', 'id', 'cused', 'callc', 'mused', 'mallc'] result = [['1/0', 'aaa', '10', '0.1', '15', '10.73', '16.00'], ['1/0', 'bbb', '10', '0.1', '20', '11.27', '14.00'], ['1/0', 'ccc', '10', '0.2', '10', '11.50', '15.00'], ['1/0', 'aaa', '10', '1.1', '15', '15.10', '23.00']] #i get: [['1/0', '1/0', '1/0', '1/0'], ['aaa', 'bbb', 'ccc', 'aaa'], .... transpose_result = map(list, zip(*result)) #i get: {'slot': ['1/0', '1/0', '1/0', '1/0'], # 'mallc': ['16.00', '14.00', '15.00', '23.00'], ... data = dict(zip(headers, transpose_result)) d = {} reg in ("cused", "callc", "mused", "mallc"): i, val in enumerate(data[reg]): key = [] reg2 in ("slot", "name", "id"): key.append(reg2) key.append(data[reg2][i]) key.append(reg) d[".".join(key)] = val you in d
{ 'slot.1/0.name.bbb.id.10.cused': '0.1', 'slot.1/0.name.aaa.id.10.cused': '1.1', 'slot.1/0.name.bbb.id.10.callc': '20', 'slot.1/0.name.aaa.id.10.mallc': '23.00', 'slot.1/0.name.aaa.id.10.callc': '15', 'slot.1/0.name.ccc.id.10.mallc': '15.00', 'slot.1/0.name.ccc.id.10.mused': '11.50', 'slot.1/0.name.aaa.id.10.mused': '15.10', 'slot.1/0.name.ccc.id.10.cused': '0.2', 'slot.1/0.name.ccc.id.10.callc': '10', 'slot.1/0.name.bbb.id.10.mallc': '14.00', 'slot.1/0.name.bbb.id.10.mused': '11.27'}
Comments
Post a Comment