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
6044655d
Kaydet (Commit)
6044655d
authored
Mar 28, 2010
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Update itertools recipes.
üst
3522e040
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
7 deletions
+27
-7
itertools.rst
Doc/library/itertools.rst
+27
-7
No files found.
Doc/library/itertools.rst
Dosyayı görüntüle @
6044655d
...
@@ -632,9 +632,6 @@ which incur interpreter overhead.
...
@@ -632,9 +632,6 @@ which incur interpreter overhead.
"Return first n items of the iterable as a list"
"Return first n items of the iterable as a list"
return list(islice(iterable, n))
return list(islice(iterable, n))
def enumerate(iterable, start=0):
return izip(count(start), iterable)
def tabulate(function, start=0):
def tabulate(function, start=0):
"Return function(0), function(1), ..."
"Return function(0), function(1), ..."
return imap(function, count(start))
return imap(function, count(start))
...
@@ -741,10 +738,9 @@ which incur interpreter overhead.
...
@@ -741,10 +738,9 @@ which incur interpreter overhead.
seen = set()
seen = set()
seen_add = seen.add
seen_add = seen.add
if key is None:
if key is None:
for element in iterable:
for element in ifilterfalse(seen.__contains__, iterable):
if element not in seen:
seen_add(element)
seen_add(element)
yield element
yield element
else:
else:
for element in iterable:
for element in iterable:
k = key(element)
k = key(element)
...
@@ -758,6 +754,30 @@ which incur interpreter overhead.
...
@@ -758,6 +754,30 @@ which incur interpreter overhead.
# unique_justseen('ABBCcAD', str.lower) --> A B C A D
# unique_justseen('ABBCcAD', str.lower) --> A B C A D
return imap(next, imap(itemgetter(1), groupby(iterable, key)))
return imap(next, imap(itemgetter(1), groupby(iterable, key)))
def iter_except(func, exception, first=None):
""" Call a function repeatedly until an exception is raised.
Converts a call-until-exception interface to an iterator interface.
Like __builtin__.iter(func, sentinel) but uses an exception instead
of a sentinel to end the loop.
Examples:
bsddbiter = iter_except(db.next, bsddb.error, db.first)
heapiter = iter_except(functools.partial(heappop, h), IndexError)
dictiter = iter_except(d.popitem, KeyError)
dequeiter = iter_except(d.popleft, IndexError)
queueiter = iter_except(q.get_nowait, Queue.Empty)
setiter = iter_except(s.pop, KeyError)
"""
try:
if first is not None:
yield first()
while 1:
yield func()
except exception:
pass
Note, many of the above recipes can be optimized by replacing global lookups
Note, many of the above recipes can be optimized by replacing global lookups
with local variables defined as default values. For example, the
with local variables defined as default values. For example, the
*dotproduct* recipe can be written as::
*dotproduct* recipe can be written as::
...
...
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