/
Oxanita
/
Homework_1
Обзор
Документация
Войти
/
Oxanita
/
Homework_1
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/counter.py
44 строки
2 KB
Oxana-Bobrysheva
Some tests were fixed
30 окт 2024, 16:53
30 окт 2024, 16:53
4570766
Код
Авторство
О чём код?
import json from collections import Counter def get_categories(list_of_transactions: list) -> list: """Function that makes list of categories in description of transactions""" list_of_categories = set() for transaction in list_of_transactions: category = transaction.get("description") if category: list_of_categories.add(category) return list(list_of_categories) def count_transactions_by_category(list_of_transactions: list, list_of_categories: list) -> dict: """Function that takes list of transactions and list of categories and returns quantity of each category in the list of transactions""" # Создаем счетчик для подсчета операций category_counter: Counter = Counter() # Итерируемся по всем операциям for transaction in list_of_transactions: # Проверяем, если описание содержит категорию for category in list_of_categories: if category in transaction.get("description", ""): category_counter[category] += 1 if not any(category_counter.values()): return {} # Преобразуем счетчик в словарь и возвращаем return {category: category_counter.get(category, 0) for category in list_of_categories} # Пример использования if __name__ == "__main__": with open("../data/operations.json", "r", encoding="UTF-8") as file: data = json.load(file) categories = get_categories(data) print(categories) result = count_transactions_by_category(data, categories) print(result)