/
volkozaq
/
Filework
Обзор
Документация
Войти
/
volkozaq
/
Filework
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main2.py
47 строк
1 KB
volkozaq
upload files
18 сен 2025, 23:39
18 сен 2025, 23:39
0ec16cc
Код
Авторство
О чём код?
with open('recipes.txt', encoding='utf-8') as f: text = f.read() food_list = text.split('\n\n') cook_book = {} key = None value = None for food in food_list: food = food.split('\n') key = food[0] value = food[2:] ingr_list = [] ingr_dict = {} for i, ingrs in enumerate(value): b = ingrs.split(' | ') ingr_dict[i] = {'ingredient_name': b[0], 'quantity': b[1], 'measure': b[2]} ingr_list.append(ingr_dict[i]) cook_book[key] = ingr_list print(cook_book) def join_dicts(list_of_dict): joined_dict = {} for dicts in list_of_dict: for key, value in dicts.items(): if key in joined_dict: dicts[key]['quantity'] += joined_dict[key]['quantity'] joined_dict.update(dicts) print(joined_dict) def get_shop_list_by_dishes(dishes, person_count): dict_list = [] for i, d in enumerate(dishes): cook_dict = {} if d in cook_book: for ing in cook_book[d]: cook_dict[ing.get('ingredient_name')] = ing for lv in cook_dict.values(): del lv['ingredient_name'] dict_list.append(cook_dict) for key, value in dict_list[i].items(): value['quantity'] = int(value['quantity']) * person_count join_dicts(dict_list) get_shop_list_by_dishes(['Омлет', 'Фахитос'], 3)