wzwwzw
2021-01-16 23:43:55 +08:00
from itertools import groupby
before_data = {
"child": [
{"f_brandname": "奔驰", "f_pyfirstletter": "B", "seriesname": "载货车"},
{"f_brandname": "北奔重卡", "f_pyfirstletter": "B", "seriesname": "载货车"},
{"f_brandname": "福田欧曼", "f_pyfirstletter": "F", "seriesname": "载货车"},
{"f_brandname": "江铃汽车", "f_pyfirstletter": "J", "seriesname": "载货车"},
]
}
after_data = {"child": {}}
for j, i in groupby(before_data["child"], key=lambda x: x["f_pyfirstletter"]):
if j not in after_data["child"]:
after_data_example["child"][j] = []
for k in i:
after_data_example["child"][j].append(k)
print(after_data_example)