Объединение словаря на основе совпадения условий
У меня есть список мультисловаря в настоящее время выглядит как
"results": [
{
"message_code": 1002,
"module": "Farm Added",
"title": {
"en": "Farm Added Successfully!"
},
"message": {
"en": "Time to add Crops to your farm!"
},
"icon_link": {
"en": null
},
"image_link": {
"en": null
}
},
{
"message_code": 1002,
"module": "Farm Added",
"title": {
"hi": "खेत सफलतापूर्वक जुड़ गया है!"
},
"message": {
"hi": "अभी अपने खेत में फसल जोड़ें|"
},
"icon_link": {
"hi": null
},
"image_link": {
"hi": null
}
},
{
"message_code": 1003,
"module": "Pending task today",
"title": {
"en": "Check out today's Tasks NOW!"
},
"message": {
"en": "Tasks are important for your crop health!"
},
"icon_link": {
"en": null
},
"image_link": {
"en": null
}
},
{
"message_code": 1003,
"module": "Pending task today",
"title": {
"hi": "आज का कार्य अभी देखें!"
},
"message": {
"hi": "आपकी फसल के स्वास्थ्य के लिए कार्य महत्वपूर्ण हैं!"
},
"icon_link": {
"hi": null
},
"image_link": {
"hi": null
}
},
Как объединить столбцы словарей "title", "image", "icon" и "message" на основе условия, когда "message_code" совпадает с другим словарем "message_code"
и иметь желаемый выход в виде
"results": [
{
"message_code": 1002,
"module": "Farm Added",
"title": {
"en": "Farm Added Successfully!",
"hi": "खेत सफलतापूर्वक जुड़ गया है!"
},
"message": {
"en": "Time to add Crops to your farm!"
"hi": "अभी अपने खेत में फसल जोड़ें|"
},
"icon_link": {
"en": null,
"hi": null
},
"image_link": {
"en": null,
"hi": null
}
},
{
"message_code": 1003,
"module": "Pending task today",
"title": {
"en": "Check out today's Tasks NOW!",
"hi": "आज का कार्य अभी देखें!"
},
"message": {
"en": "Tasks are important for your crop health!",
"hi": "आपकी फसल के स्वास्थ्य के लिए कार्य महत्वपूर्ण हैं!"
},
"icon_link": {
"en": null,
"hi": null
},
"image_link": {
"en": null,
"hi": null
}
},
Код для этого, который я использую в Django, получает API
notification =[]
notification_data = get_notification_template(filter)
notification_data = notification_data.replace({np.nan: None})
for index, row in notification_data.iterrows():
input_dict = {
'message_code': row['message_code'],
'module': row['action_at'],
'title': {row['language_code']: row['title']},
'message': {row['language_code']: row['message']},
'icon_link': {row['language_code']: row['icon_link']},
'image_link': {row['language_code']: row['image_link']},
}
notification.append(input_dict)
Здесь get_notification_template - это функция, которая получает исходные данные из базы данных и здесь я создаю словарь для отображения данных в соответствии с требуемым форматом