Erweiterte Funktionen
Lambda-Funktionen vertiefen
map() Funktion
# Grundlegende Verwendung
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x ** 2, numbers))
print(squared) # [1, 4, 9, 16, 25]
# Mit regulärer Funktion vergleichen
def square(x):
return x ** 2
squared2 = list(map(square, numbers))
print(squared2) # [1, 4, 9, 16, 25]
# Mehrere Argumente
def add(x, y):
return x + y
a = [1, 2, 3]
b = [10, 20, 30]
result = list(map(add, a, b))
print(result) # [11, 22, 33]
# Mit Lambda
result2 = list(map(lambda x, y: x + y, a, b))
print(result2) # [11, 22, 33]
# Praktisches Beispiel
prices = [100, 200, 300, 400]
with_tax = list(map(lambda x: x * 1.1, prices))
print(with_tax) # [110.0, 220.0, 330.0, 440.0]
# Zeichenkettenverarbeitung
names = ["alice", "bob", "charlie"]
upper_names = list(map(str.upper, names))
print(upper_names) # ['ALICE', 'BOB', 'CHARLIE']
# Komplexe Transformation
users = [
{"name": "Kim Min-ji", "age": 25},
{"name": "Park Sung-ho", "age": 30}
]
names_only = list(map(lambda u: u["name"], users))
print(names_only) # ['Kim Min-ji', 'Park Sung-ho']
[Restliche Inhalte werden genau wie im Originaltext übersetzt, mit Beibehaltung der Code-Blöcke und der technischen Formatierung.]