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
0f3ec6dd
Commit
0f3ec6dd
authored
Apr 02, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix nits in itertools recipes.
parent
063a4b68
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
5 additions
and
5 deletions
+5
-5
Doc/library/itertools.rst
Doc/library/itertools.rst
+5
-5
No files found.
Doc/library/itertools.rst
View file @
0f3ec6dd
...
...
@@ -611,7 +611,7 @@ which incur interpreter overhead.
def ncycles(iterable, n):
"Returns the sequence elements n times"
return chain.from_iterable(repeat(
iterable
, n))
return chain.from_iterable(repeat(
tuple(iterable)
, n))
def dotproduct(vec1, vec2):
return sum(map(operator.mul, vec1, vec2))
...
...
@@ -707,23 +707,23 @@ which incur interpreter overhead.
def random_product(*args, repeat=1):
"Random selection from itertools.product(*args, **kwds)"
pools = [tuple(pool) for pool in args] * repeat
return
[random.choice(pool) for pool in pools]
return
tuple(random.choice(pool) for pool in pools)
def random_permuation(iterable, r=None):
"Random selection from itertools.permutations(iterable, r)"
pool = tuple(iterable)
r = len(pool) if r is None else r
return
random.sample(pool, r
)
return
tuple(random.sample(pool, r)
)
def random_combination(iterable, r):
"Random selection from itertools.combinations(iterable, r)"
pool = tuple(iterable)
return
sorted(random.sample(pool, r), key=pool.index
)
return
tuple(sorted(random.sample(pool, r), key=pool.index)
)
def random_combination_with_replacement(iterable, r):
"Random selection from itertools.combinations_with_replacement(iterable, r)"
pool = tuple(iterable)
return
sorted(map(random.choice, repeat(pool, r)), key=pool.index
)
return
tuple(sorted(map(random.choice, repeat(pool, r)), key=pool.index)
)
Note, many of the above recipes can be optimized by replacing global lookups
with local variables defined as default values. For example, the
...
...
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