mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
merge
This commit is contained in:
commit
0ac8e47149
2 changed files with 29 additions and 0 deletions
|
@ -784,6 +784,19 @@ which incur interpreter overhead.
|
|||
except exception:
|
||||
pass
|
||||
|
||||
def first_true(iterable, default=False, pred=None):
|
||||
"""Returns the first true value in the iterable.
|
||||
|
||||
If no true value is found, returns *default*
|
||||
|
||||
If *pred* is not None, returns the first item
|
||||
for which pred(item) is true.
|
||||
|
||||
"""
|
||||
# first_true([a,b,c], x) --> a or b or c or x
|
||||
# first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
|
||||
return next(filter(pred, iterable), default)
|
||||
|
||||
def random_product(*args, repeat=1):
|
||||
"Random selection from itertools.product(*args, **kwds)"
|
||||
pools = [tuple(pool) for pool in args] * repeat
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue