Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
31b26f63
Kaydet (Commit)
31b26f63
authored
Nis 02, 2014
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #18652: Add an itertools recipe for first_true()
üst
f4284e45
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
0 deletions
+29
-0
itertools.rst
Doc/library/itertools.rst
+13
-0
test_itertools.py
Lib/test/test_itertools.py
+16
-0
No files found.
Doc/library/itertools.rst
Dosyayı görüntüle @
31b26f63
...
@@ -784,6 +784,19 @@ which incur interpreter overhead.
...
@@ -784,6 +784,19 @@ which incur interpreter overhead.
except exception:
except exception:
pass
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):
def random_product(*args, repeat=1):
"Random selection from itertools.product(*args, **kwds)"
"Random selection from itertools.product(*args, **kwds)"
pools = [tuple(pool) for pool in args] * repeat
pools = [tuple(pool) for pool in args] * repeat
...
...
Lib/test/test_itertools.py
Dosyayı görüntüle @
31b26f63
...
@@ -1998,6 +1998,19 @@ Samuele
...
@@ -1998,6 +1998,19 @@ Samuele
... # unique_justseen('ABBCcAD', str.lower) --> A B C A D
... # unique_justseen('ABBCcAD', str.lower) --> A B C A D
... return map(next, map(itemgetter(1), groupby(iterable, key)))
... return map(next, map(itemgetter(1), groupby(iterable, key)))
>>> 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)
This is not part of the examples but it tests to make sure the definitions
This is not part of the examples but it tests to make sure the definitions
perform as purported.
perform as purported.
...
@@ -2075,6 +2088,9 @@ True
...
@@ -2075,6 +2088,9 @@ True
>>> list(unique_justseen('ABBCcAD', str.lower))
>>> list(unique_justseen('ABBCcAD', str.lower))
['A', 'B', 'C', 'A', 'D']
['A', 'B', 'C', 'A', 'D']
>>> first_true('ABC0DEF1', '9', str.isdigit)
'0'
"""
"""
__test__
=
{
'libreftest'
:
libreftest
}
__test__
=
{
'libreftest'
:
libreftest
}
...
...
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