mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
Part of SF patch #1513870 (the still relevant part) -- add reduce() to
functools, and adjust docs etc.
This commit is contained in:
parent
6a2a2a0832
commit
0919a1a07b
11 changed files with 168 additions and 78 deletions
|
@ -289,19 +289,7 @@ There are also many useful builtin functions people seem not to be
|
|||
aware of for some reason: \function{min()} and \function{max()} can
|
||||
find the minimum/maximum of any sequence with comparable semantics,
|
||||
for example, yet many people write their own
|
||||
\function{max()}/\function{min()}. Another highly useful function is
|
||||
\function{reduce()}. A classical use of \function{reduce()}
|
||||
is something like
|
||||
|
||||
\begin{verbatim}
|
||||
import sys, operator
|
||||
nums = map(float, sys.argv[1:])
|
||||
print reduce(operator.add, nums)/len(nums)
|
||||
\end{verbatim}
|
||||
|
||||
This cute little script prints the average of all numbers given on the
|
||||
command line. The \function{reduce()} adds up all the numbers, and
|
||||
the rest is just some pre- and postprocessing.
|
||||
\function{max()}/\function{min()}.
|
||||
|
||||
On the same note, note that \function{float()}, \function{int()} and
|
||||
\function{long()} all accept arguments of type string, and so are
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue