-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlista_cuadrados.py
More file actions
64 lines (54 loc) · 2.81 KB
/
lista_cuadrados.py
File metadata and controls
64 lines (54 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
List Comprehensions and Filtering
====================================
Common patterns used in data wrangling and pipeline transformations.
Comprensiones de listas y filtrado
=====================================
Patrones comunes en limpieza de datos y transformaciones de pipelines.
"""
# ---------------------------------------------------------------------------
# 1. Multiples of 12 up to 1000 / Multiplos de 12 hasta 1000
# (original: multiples common to 4, 6, and 9)
# ---------------------------------------------------------------------------
multiples_of_12 = [n for n in range(1, 1001) if n % 12 == 0]
print(f"Multiples of 12 up to 1,000 / Multiplos de 12 hasta 1,000:")
print(f" Count: {len(multiples_of_12)} | First 5: {multiples_of_12[:5]} | Last 5: {multiples_of_12[-5:]}")
# ---------------------------------------------------------------------------
# 2. Even squares / Cuadrados de numeros pares
# ---------------------------------------------------------------------------
even_squares = [n ** 2 for n in range(1, 21) if n % 2 == 0]
print(f"\nEven squares (1-20) / Cuadrados pares (1-20):")
print(f" {even_squares}")
# ---------------------------------------------------------------------------
# 3. Filtering and transforming records / Filtrado y transformacion
# ---------------------------------------------------------------------------
transactions = [
{"product": "Laptop Pro", "units": 12, "price": 1200.0},
{"product": "Monitor 4K", "units": 0, "price": 450.0},
{"product": "Keyboard", "units": 35, "price": 85.0},
{"product": "USB Hub", "units": 0, "price": 28.5},
{"product": "Webcam HD", "units": 20, "price": 95.0},
{"product": "Desk Lamp", "units": 5, "price": 40.0},
]
# Only active products / Solo productos con ventas activas
active = [t for t in transactions if t["units"] > 0]
# Revenue per product / Ingreso por producto
revenue = [
{"product": t["product"], "revenue": round(t["units"] * t["price"], 2)}
for t in active
]
print("\nRevenue per active product / Ingreso por producto activo:")
for item in sorted(revenue, key=lambda x: x["revenue"], reverse=True):
print(f" {item['product']:<16}: ${item['revenue']:>8,.2f}")
total = sum(item["revenue"] for item in revenue)
print(f"\n Total revenue / Ingreso total: ${total:>10,.2f}")
# ---------------------------------------------------------------------------
# 4. Flattening nested lists / Aplanando listas anidadas
# ---------------------------------------------------------------------------
weekly_sales = [[120, 95, 110], [88, 102, 134], [99, 115, 107]]
flat_sales = [day for week in weekly_sales for day in week]
print(f"\nFlattened daily sales / Ventas diarias aplanadas:")
print(f" {flat_sales}")
print(f" Average / Promedio: {sum(flat_sales)/len(flat_sales):.1f}")
if __name__ == "__main__":
pass