first commit
This commit is contained in:
commit
d63ada8bfa
7
data/orders.txt
Normal file
7
data/orders.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
1,2500,Москва,card
|
||||||
|
2,1300,Казань,cash
|
||||||
|
1,800,Москва,card
|
||||||
|
3,5000,СПб,card
|
||||||
|
2,450,Казань,cash
|
||||||
|
1,1200,Москва,card
|
||||||
|
4,3000,СПБ,crypto
|
||||||
134
orders_analysis.py
Normal file
134
orders_analysis.py
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
import os
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
|
||||||
|
def load_data(filepath: str) -> list[dict]:
|
||||||
|
orders = []
|
||||||
|
try:
|
||||||
|
with open(filepath, 'r', encoding='utf-8') as file:
|
||||||
|
for line_num, line in enumerate(file, 1):
|
||||||
|
line = line.strip()
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
parts = line.split(',')
|
||||||
|
if len(parts) != 4:
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
user_id = int(parts[0].strip())
|
||||||
|
order_total = float(parts[1].strip())
|
||||||
|
city = parts[2].strip().lower()
|
||||||
|
payment_method = parts[3].strip().lower()
|
||||||
|
|
||||||
|
orders.append({
|
||||||
|
'user_id': user_id,
|
||||||
|
'order_total': order_total,
|
||||||
|
'city': city,
|
||||||
|
'payment_method': payment_method
|
||||||
|
})
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
|
||||||
|
except FileNotFoundError:
|
||||||
|
return []
|
||||||
|
|
||||||
|
return orders
|
||||||
|
|
||||||
|
|
||||||
|
def filter_by_city(orders: list[dict], city: str) -> list[dict]:
|
||||||
|
city_lower = city.lower()
|
||||||
|
return [order for order in orders if order['city'] == city_lower]
|
||||||
|
|
||||||
|
|
||||||
|
def filter_by_min_total(orders: list[dict], min_total: float) -> list[dict]:
|
||||||
|
return [order for order in orders if order['order_total'] >= min_total]
|
||||||
|
|
||||||
|
|
||||||
|
def get_unique_users(orders: list[dict]) -> list[int]:
|
||||||
|
return list(set(order['user_id'] for order in orders))
|
||||||
|
|
||||||
|
|
||||||
|
def total_revenue(orders: list[dict]) -> float:
|
||||||
|
return sum(order['order_total'] for order in orders)
|
||||||
|
|
||||||
|
|
||||||
|
def average_order_value(orders: list[dict]) -> float:
|
||||||
|
if not orders:
|
||||||
|
return 0.0
|
||||||
|
return total_revenue(orders) / len(orders)
|
||||||
|
|
||||||
|
|
||||||
|
def payment_method_stats(orders: list[dict]) -> dict[str, int]:
|
||||||
|
stats = defaultdict(int)
|
||||||
|
for order in orders:
|
||||||
|
stats[order['payment_method']] += 1
|
||||||
|
return dict(stats)
|
||||||
|
|
||||||
|
|
||||||
|
def city_revenue(orders: list[dict]) -> dict[str, float]:
|
||||||
|
revenue = defaultdict(float)
|
||||||
|
for order in orders:
|
||||||
|
revenue[order['city']] += order['order_total']
|
||||||
|
return dict(revenue)
|
||||||
|
|
||||||
|
|
||||||
|
def top_users_by_spent(orders: list[dict], top_n: int) -> list[tuple[int, float]]:
|
||||||
|
user_spent = defaultdict(float)
|
||||||
|
for order in orders:
|
||||||
|
user_spent[order['user_id']] += order['order_total']
|
||||||
|
|
||||||
|
sorted_users = sorted(user_spent.items(), key=lambda x: x[1], reverse=True)
|
||||||
|
return sorted_users[:top_n]
|
||||||
|
|
||||||
|
|
||||||
|
def filter_top_cities_by_orders(orders: list[dict], min_orders: int) -> list[str]:
|
||||||
|
city_orders = defaultdict(int)
|
||||||
|
for order in orders:
|
||||||
|
city_orders[order['city']] += 1
|
||||||
|
|
||||||
|
filtered_cities = [(city, count) for city, count in city_orders.items() if count >= min_orders]
|
||||||
|
filtered_cities.sort(key=lambda x: x[1], reverse=True)
|
||||||
|
|
||||||
|
return [city for city, _ in filtered_cities]
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
filepath = "data/orders.txt"
|
||||||
|
|
||||||
|
orders = load_data(filepath)
|
||||||
|
if not orders:
|
||||||
|
return
|
||||||
|
|
||||||
|
revenue = total_revenue(orders)
|
||||||
|
print(f"Общая выручка: {revenue:.2f} руб.")
|
||||||
|
|
||||||
|
avg_order = average_order_value(orders)
|
||||||
|
print(f"Средняя сумма заказа: {avg_order:.2f} руб.")
|
||||||
|
|
||||||
|
payment_stats = payment_method_stats(orders)
|
||||||
|
print("Статистика по методам оплаты:")
|
||||||
|
for method, count in payment_stats.items():
|
||||||
|
print(f" {method}: {count}")
|
||||||
|
|
||||||
|
top_users = top_users_by_spent(orders, 2)
|
||||||
|
print("Топ-2 пользователей по сумме трат:")
|
||||||
|
for user_id, spent in top_users:
|
||||||
|
print(f" Пользователь {user_id}: {spent:.2f} руб.")
|
||||||
|
|
||||||
|
filtered_orders = filter_by_min_total(orders, 1000)
|
||||||
|
if filtered_orders:
|
||||||
|
city_rev = city_revenue(filtered_orders)
|
||||||
|
print("Выручка по городам (заказы >= 1000 руб.):")
|
||||||
|
for city, rev in sorted(city_rev.items(), key=lambda x: x[1], reverse=True):
|
||||||
|
print(f" {city.capitalize()}: {rev:.2f} руб.")
|
||||||
|
|
||||||
|
top_cities = filter_top_cities_by_orders(orders, 2)
|
||||||
|
if top_cities:
|
||||||
|
print("Города с количеством заказов >= 2:")
|
||||||
|
for city in top_cities:
|
||||||
|
print(f" {city.capitalize()}")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in New Issue
Block a user