🎭 Map, Filter, Reduce - Bộ ba siêu anh hùng của Python
thông tin
Map, Filter, Reduce giống như một nhóm siêu anh hùng - mỗi người có một sức mạnh đặc biệt để xử lý dữ liệu!
🤔 Tại sao cần Map, Filter, Reduce?
Hãy tưởng tượng bạn có một danh sách sinh viên và cần:
- Map 🗺️: Chuyển đổi mỗi phần tử (như một máy biến đổi)
- Filter 🔍: Lọc các phần tử theo điều kiện (như một cái rây)
- Reduce ⚡: Kết hợp tất cả thành một kết quả (như một máy nén)
# Ví dụ minh họa
scores = [7, 8.5, 6, 9.2, 5.5, 8.8, 7.5]
# Map: Chuyển điểm thành thang 4
gpa_scores = list(map(lambda x: round(x / 2.5, 1), scores))
print(f"🗺️ Map - Thang 4.0: {gpa_scores}")
# Filter: Lọc điểm >= 8
high_scores = list(filter(lambda x: x >= 8, scores))
print(f"🔍 Filter - Điểm cao: {high_scores}")
# Reduce: Tính điểm trung bình
from functools import reduce
average_score = reduce(lambda x, y: x + y, scores) / len(scores)
print(f"⚡ Reduce - Điểm TB: {average_score:.1f}")
🗺️ MAP - Máy biến đổi thần kỳ
Map áp dụng một hàm cho TẤT CẢ phần tử trong iterable.
Cú pháp và cách dùng
# map(function, iterable)
# Trả về map object (cần convert thành list để xem)
# Ví dụ cơ bản
numbers = [1, 2, 3, 4, 5]
# Bình phương tất cả
squared_numbers = list(map(lambda x: x**2, numbers))
print(f"📊 Bình phương: {squared_numbers}") # [1, 4, 9, 16, 25]
# Với function định sẵn
def convert_to_text(number):
"""Chuyển số thành chữ"""
digit_names = ["Không", "Một", "Hai", "Ba", "Bốn", "Năm"]
return digit_names[number] if 0 <= number < len(digit_names) else str(number)
text_numbers = list(map(convert_to_text, numbers))
print(f"🔤 Thành chữ: {text_numbers}")
Map với nhiều iterables
# Map với 2 lists
list_a = [1, 2, 3, 4]
list_b = [10, 20, 30, 40]
# Cộng từng cặp
pair_sums = list(map(lambda x, y: x + y, list_a, list_b))
print(f"➕ Tổng các cặp: {pair_sums}") # [11, 22, 33, 44]
# Nhân từng cặp
pair_products = list(map(lambda x, y: x * y, list_a, list_b))
print(f"✖️ Nhân các cặp: {pair_products}") # [10, 40, 90, 160]
# Map với 3 lists
names = ["An", "Bình", "Cường"]
ages = [20, 19, 21]
scores = [8.5, 9.0, 7.8]
student_info = list(map(
lambda name, age, score: f"{name} ({age} tuổi) - {score} điểm",
names, ages, scores
))
print("👥 Thông tin sinh viên:")
for info in student_info:
print(f" 📝 {info}")
Map với String processing
# Xử lý chuỗi
sentences = [
"python là tuyệt vời",
"học lập trình rất thú vị",
"ai và machine learning"
]
# Capitalize từng từ
formatted_sentences = list(map(lambda sentence: sentence.title(), sentences))
print("✨ Formatted sentences:")
for sentence in formatted_sentences:
print(f" 📄 {sentence}")
# Đếm từ trong mỗi câu
word_counts = list(map(lambda sentence: len(sentence.split()), sentences))
print(f"📊 Số từ: {word_counts}")
# Thay thế từ khóa
replaced_sentences = list(map(
lambda sentence: sentence.replace("python", "Python 🐍").replace("ai", "AI 🤖"),
sentences
))
print("🔄 Sau khi thay thế:")
for sentence in replaced_sentences:
print(f" 🎯 {sentence}")
🔍 FILTER - Cái rây thông minh
Filter chỉ giữ lại những phần tử thỏa mãn điều kiện.
Cú pháp và cách dùng
# filter(function, iterable)
# function trả về True/False
# Lọc số chẵn
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(f"🔢 Số chẵn: {even_numbers}") # [2, 4, 6, 8, 10]
# Lọc số nguyên tố đơn giản
def is_prime(n):
"""Kiểm tra số nguyên tố"""
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
prime_numbers = list(filter(is_prime, range(2, 30)))
print(f"🔍 Số nguyên tố < 30: {prime_numbers}")
# Lọc chuỗi không rỗng
string_list = ["Python", "", "Java", " ", "C++", None, "Go"]
valid_strings = list(filter(
lambda s: s and s.strip(),
[s for s in string_list if s is not None]
))
print(f"📝 Chuỗi hợp lệ: {valid_strings}")
Filter với Objects
# Danh sách sản phẩm
products = [
{"name": "Laptop", "price": 15000000, "stock": 5, "category": "electronics"},
{"name": "Mouse", "price": 200000, "stock": 0, "category": "electronics"},
{"name": "Áo thun", "price": 150000, "stock": 20, "category": "clothing"},
{"name": "Giày", "price": 800000, "stock": 8, "category": "clothing"},
{"name": "Sách", "price": 50000, "stock": 15, "category": "books"}
]
# Lọc sản phẩm còn hàng
in_stock_products = list(filter(lambda product: product["stock"] > 0, products))
print(f"📦 Sản phẩm còn hàng: {len(in_stock_products)}/{len(products)}")
# Lọc sản phẩm electronics đắt tiền
expensive_electronics = list(filter(
lambda product: product["category"] == "electronics" and product["price"] >= 500000,
products
))
print("💻 Electronics đắt tiền:")
for product in expensive_electronics:
print(f" 🛍️ {product['name']}: {product['price']:,}đ")
# Lọc theo nhiều điều kiện
bestsellers = list(filter(
lambda product: product["stock"] > 10 and product["price"] <= 500000,
products
))
print("🔥 Sản phẩm bán chạy (nhiều hàng + giá rẻ):")
for product in bestsellers:
print(f" ⭐ {product['name']}: {product['price']:,}đ (còn {product['stock']})")
⚡ REDUCE - Máy nén siêu mạnh
Reduce kết hợp tất cả phần tử thành một giá trị duy nhất.
Cú pháp và cách dùng
from functools import reduce
# reduce(function, iterable[, initializer])
# function nhận 2 tham số: accumulator và current_item
# Tính tổng
numbers = [1, 2, 3, 4, 5]
total = reduce(lambda x, y: x + y, numbers)
print(f"➕ Tổng: {total}") # 15
# Tính tích
product = reduce(lambda x, y: x * y, numbers)
print(f"✖️ Tích: {product}") # 120
# Tìm số lớn nhất
max_number = reduce(lambda x, y: x if x > y else y, numbers)
print(f"🔝 Max: {max_number}") # 5
# Với initial value
total_with_initial = reduce(lambda x, y: x + y, numbers, 100)
print(f"➕ Tổng + 100: {total_with_initial}") # 115
Reduce nâng cao
# Nối chuỗi
words = ["Python", "is", "really", "awesome"]
sentence = reduce(lambda x, y: x + " " + y, words)
print(f"📝 Câu: {sentence}")
# Tạo dictionary từ lists
keys = ["name", "age", "score"]
values = ["An", 20, 8.5]
student_dict = reduce(
lambda d, kv: {**d, kv[0]: kv[1]},
zip(keys, values),
{}
)
print(f"👤 Sinh viên: {student_dict}")
# Flatten nested list
nested_list = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
flat_list = reduce(lambda x, y: x + y, nested_list)
print(f"📋 Flattened: {flat_list}")
# Tính factorial
def calculate_factorial(n):
"""Tính giai thừa"""
if n <= 1:
return 1
return reduce(lambda x, y: x * y, range(1, n + 1))
print(f"🧮 5! = {calculate_factorial(5)}") # 120
print(f"🧮 7! = {calculate_factorial(7)}") # 5040
🎭 Kết hợp Map, Filter, Reduce
# Dữ liệu bán hàng
sales_data = [
{"employee": "An", "orders": [100000, 200000, 150000]},
{"employee": "Bình", "orders": [300000, 250000, 400000]},
{"employee": "Cường", "orders": [180000, 220000]},
{"employee": "Dung", "orders": [500000, 120000, 300000, 180000]}
]
print("🏪 Phân tích doanh số bán hàng")
print("="*50)
# Bước 1: Map - Tính tổng doanh thu mỗi nhân viên
employee_revenues = list(map(
lambda emp: {
"name": emp["employee"],
"total_revenue": sum(emp["orders"]),
"order_count": len(emp["orders"])
},
sales_data
))
for emp in employee_revenues:
print(f"👤 {emp['name']}: {emp['total_revenue']:,}đ ({emp['order_count']} đơn)")
# Bước 2: Filter - Lọc nhân viên doanh thu cao
top_performers = list(filter(
lambda emp: emp["total_revenue"] >= 400000,
employee_revenues
))
print(f"\n🌟 Nhân viên xuất sắc (≥400k):")
for emp in top_performers:
avg_order = emp["total_revenue"] // emp["order_count"]
print(f" 🏆 {emp['name']}: {emp['total_revenue']:,}đ (TB: {avg_order:,}đ/đơn)")
# Bước 3: Reduce - Tính tổng doanh thu công ty
company_total = reduce(
lambda total, emp: total + emp["total_revenue"],
employee_revenues,
0
)
print(f"\n💰 Tổng doanh thu công ty: {company_total:,}đ")
# Combo: Tính phần trăm đóng góp
employee_percentages = list(map(
lambda emp: {
**emp,
"percentage": round(emp["total_revenue"] / company_total * 100, 1)
},
employee_revenues
))
print(f"\n📊 Phần trăm đóng góp:")
for emp in employee_percentages:
print(f" 📈 {emp['name']}: {emp['percentage']}%")