У меня сложная задача. У меня есть массив словарей, структура выглядит так:
update_logics = [
{
"dependency_fields": [
"population",
"field_1",
],
"update_logic": "{return CurrentState['population'] / CurrentState['area_km2'];}",
"updated_field": "field_2",
},
{
"dependency_fields": [
"leader",
"fields_2",
],
"update_logic": "{return 'capital is: '+ CurrentState['capital'];}",
"updated_field": "field_3",
},
{
"dependency_fields": [
"leader",
"fields_3",
],
"update_logic": "{return 'capital is: '+ CurrentState['capital'];}",
"updated_field": "field_1",
},
]
Я хотел бы проверить каждый updated_field в массиве. updated_field зависит от зависимых полей
Если у меня круговая зависимость, я хотел бы вернуть False или любую отметку. Если он не циклический, я бы хотел получить глубину зависимостей.
Я думаю, рекурсия здесь полезна, но я не понимаю, как работать с массивами:
def check(arr, parent, el):
for item in arr:
item["updated_field"] ==el
for child in item["dependency_fields"]:
if child == el or child == parent:
# circular
return False
else:
# arr_for_check.append(child)
check(arr, child, el)
Так что здесь рекурсия будет работать для первого элемента в списке всегда.