d_list = [{
'a': 28.0,
'b': None,
'c': 93
}, {
'a': 25.0,
'b': [{"id":91,"qty":2,"fp":24.0000,"pp":0},
{"id":91,"qty":4, "fp":23.0000, "pp":0},
{"id":91,"qty":6, "fp":20.0000," pp":0}],
'c': 91
},
{
'a': 24.0,
'b': [{"id":191,"qty":2,"fp":24.0000,"pp":0},
{"id":191,"qty":6, "fp":20.0000," pp":0}],
'c': 191
}
]
res = [{
'a': 28.0,
'c': 93,
'qty1': False,
'fp1': False,
'qty2': False,
'fp2': False,
'qty3': False,
'fp3': False,
}, {
'a': 25.0,
'b': [{"id":91,"qty":2,"fp":24.0000,"pp":0},
{"id":91,"qty":4, "fp":23.0000, "pp":0},
{"id":91,"qty":6, "fp":20.0000," pp":0}],
'c': 91,
'qty1': 2,
'fp1': 24.0000,
'qty2': 4,
'fp2': 23.0000,
'qty3': 6,
'fp3': 20.0000,
}, {
'a': 25.0,
'b': [{"id":191,"qty":2,"fp":24.0000,"pp":0},
{"id":191,"qty":4, "fp":23.0000, "pp":0},],
'c': 191,
'qty1': 2,
'fp1': 24.0000,
'qty2': 4,
'fp2': 23.0000,
'qty3': False,
'fp3': False,
}]
d_list = [{
'a': 28.0,
'b': None,
'c': 93
}, {
'a': 25.0,
'b': [{"id": 91, "qty": 2, "fp": 24.0000, "pp": 0},
{"id": 91, "qty": 4, "fp": 23.0000, "pp": 0},
{"id": 91, "qty": 6, "fp": 20.0000, " pp": 0}],
'c': 91
},
{
'a': 24.0,
'b': [{"id": 191, "qty": 2, "fp": 24.0000, "pp": 0},
{"id": 191, "qty": 6, "fp": 20.0000, " pp": 0}],
'c': 191
}
]
res = []
for d in d_list:
qty1 = False
qty2 = False
qty3 = False
fp1 = False
fp2 = False
fp3 = False
if d.get('b'):
b_list = d.get('b') # and type []
if len(b_list) >= 3:
qty3 = b_list[2]['qty']
fp3 = b_list[2]['fp']
if len(b_list) >= 2:
qty2 = b_list[1]['qty']
fp2 = b_list[1]['fp']
if len(b_list) >= 1:
qty1 = b_list[0]['qty']
fp1 = b_list[0]['fp']
res_d = d.copy()
res_d.pop('b')
res_d['qty1'] = qty1
res_d['qty2'] = qty2
res_d['qty3'] = qty3
res_d['fp1'] = fp1
res_d['fp2'] = fp2
res_d['fp3'] = fp3
res.append(res_d)
d_list
на 100.000 у меня выполнилось за 0.6868....res = []
for dictionary in d_list: # * 100_000:
result_dict = {}
for key in dictionary:
if dictionary[key] is not None:
result_dict[key] = dictionary[key]
result_dict = {**result_dict, **{'qty1': False, 'fp1': False,
'qty2': False, 'fp2': False,
'qty3': False, 'fp3': False}}
if isinstance(dictionary['b'], list):
for z in range(1, len(dictionary['b']) - 1):
result_dict[f'qty{z}'] = dictionary['b'][z - 1]['qty']
result_dict[f'fp{z}'] = dictionary['b'][z - 1]['fp']
res.append(result_dict)
b
в исходном виде.res = []
for dictionary in d_list: # * 100_000:
result_dict = {'a': dictionary['a'],
'c': dictionary['c'],
'qty1': False, 'fp1': False,
'qty2': False, 'fp2': False,
'qty3': False, 'fp3': False
}
if isinstance(dictionary['b'], list):
for z in range(1, len(dictionary['b']) - 1):
result_dict[f'qty{z}'] = dictionary['b'][z - 1]['qty']
result_dict[f'fp{z}'] = dictionary['b'][z - 1]['fp']
res.append(result_dict)