added
This commit is contained in:
1 parent
b17db7bc39
commit
d327f5d7e6
7 files changed
+132
No files matched your search
@@ -0,0 +1,16 @@
|
||||
from functools import reduce
|
||||
|
||||
numbers = [2, 3, 4, 5]
|
||||
|
||||
# Produkt berechnen
|
||||
result = reduce(lambda x, y: x * y, numbers, 1)
|
||||
|
||||
print("Produkt:", result)
|
||||
assert result == 120
|
||||
|
||||
# Verhalten bei einer leeren Liste:
|
||||
# Ohne Startwert (initializer) wirft reduce() einen TypeError: "reduce() of empty sequence with no initial value".
|
||||
# Behebung: Der optional paramter initializer=1 sorgt dafür, dass bei einer leeren Liste das neutrale Element 1 zurückgegeben wird.
|
||||
empty_list = []
|
||||
result_empty = reduce(lambda x, y: x * y, empty_list, 1)
|
||||
assert result_empty == 1
|
||||
Reference in new issue
Block a user