Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
48d1928b
Commit
48d1928b
authored
Oct 31, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue 7402: Improve reduce() example in the python idioms how-to.
parent
15c2cec4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
17 deletions
+16
-17
Doc/howto/doanddont.rst
Doc/howto/doanddont.rst
+16
-17
No files found.
Doc/howto/doanddont.rst
View file @
48d1928b
...
@@ -281,23 +281,22 @@ Compare::
...
@@ -281,23 +281,22 @@ Compare::
More useful functions in :mod:`os.path`: :func:`basename`, :func:`dirname` and
More useful functions in :mod:`os.path`: :func:`basename`, :func:`dirname` and
:func:`splitext`.
:func:`splitext`.
There are also many useful built-in functions people seem not to be aware of for
There are also many useful built-in functions people seem not to be aware of
some reason: :func:`min` and :func:`max` can find the minimum/maximum of any
for some reason: :func:`min` and :func:`max` can find the minimum/maximum of
sequence with comparable semantics, for example, yet many people write their own
any sequence with comparable semantics, for example, yet many people write
:func:`max`/:func:`min`. Another highly useful function is :func:`reduce`. A
their own :func:`max`/:func:`min`. Another highly useful function is
classical use of :func:`reduce` is something like ::
:func:`reduce` which can be used to repeatly apply a binary operation to a
sequence, reducing it to a single value. For example, compute a factorial
import sys, operator
with a series of multiply operations::
nums = map(float, sys.argv[1:])
print reduce(operator.add, nums)/len(nums)
>>> n = 4
>>> import operator
This cute little script prints the average of all numbers given on the command
>>> reduce(operator.mul, range(1, n+1))
line. The :func:`reduce` adds up all the numbers, and the rest is just some
24
pre- and postprocessing.
When it comes to parsing numbers, note that :func:`float`, :func:`int` and
On the same note, note that :func:`float`, :func:`int` and :func:`long` all
:func:`long` all accept string arguments and will reject ill-formed strings
accept arguments of type string, and so are suited to parsing --- assuming you
by raising an :exc:`ValueError`.
are ready to deal with the :exc:`ValueError` they raise.
Using Backslash to Continue Statements
Using Backslash to Continue Statements
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment