MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / reduce

Function reduce

tools/python-3.11.9-amd64/Lib/functools.py:237–263  ·  view source on GitHub ↗

reduce(function, iterable[, initial]) -> value Apply a function of two arguments cumulatively to the items of a sequence or iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates (((

(function, sequence, initial=_initial_missing)

Source from the content-addressed store, hash-verified

235_initial_missing = object()
236
237def reduce(function, sequence, initial=_initial_missing):
238 """
239 reduce(function, iterable[, initial]) -> value
240
241 Apply a function of two arguments cumulatively to the items of a sequence
242 or iterable, from left to right, so as to reduce the iterable to a single
243 value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
244 ((((1+2)+3)+4)+5). If initial is present, it is placed before the items
245 of the iterable in the calculation, and serves as a default when the
246 iterable is empty.
247 """
248
249 it = iter(sequence)
250
251 if initial is _initial_missing:
252 try:
253 value = next(it)
254 except StopIteration:
255 raise TypeError(
256 "reduce() of empty iterable with no initial value") from None
257 else:
258 value = initial
259
260 for element in it:
261 value = function(value, element)
262
263 return value
264
265try:
266 from _functools import reduce

Callers 8

_sumFunction · 0.90
_ssFunction · 0.90
__call__Method · 0.90
nth_productFunction · 0.90
nth_productFunction · 0.90
validateFunction · 0.90
detect_indentationMethod · 0.90
saveMethod · 0.70

Calls 1

functionFunction · 0.50

Tested by

no test coverage detected