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
f1f46f03
Kaydet (Commit)
f1f46f03
authored
Tem 19, 2008
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Clean-up itertools docs and recipes.
üst
39e0eb76
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
101 deletions
+36
-101
itertools.rst
Doc/library/itertools.rst
+19
-46
test_itertools.py
Lib/test/test_itertools.py
+17
-55
No files found.
Doc/library/itertools.rst
Dosyayı görüntüle @
f1f46f03
...
@@ -35,18 +35,11 @@ equivalent result.
...
@@ -35,18 +35,11 @@ equivalent result.
Likewise, the functional tools are designed to work well with the high-speed
Likewise, the functional tools are designed to work well with the high-speed
functions provided by the :mod:`operator` module.
functions provided by the :mod:`operator` module.
The module author welcomes suggestions for other basic building blocks to be
added to future versions of the module.
Whether cast in pure python form or compiled code, tools that use iterators are
Whether cast in pure python form or compiled code, tools that use iterators are
more memory efficient (and faster) than their list based counterparts. Adopting
more memory efficient (and
often
faster) than their list based counterparts. Adopting
the principles of just-in-time manufacturing, they create data when and where
the principles of just-in-time manufacturing, they create data when and where
needed instead of consuming memory with the computer equivalent of "inventory".
needed instead of consuming memory with the computer equivalent of "inventory".
The performance advantage of iterators becomes more acute as the number of
elements increases -- at some point, lists grow large enough to severely impact
memory cache performance and start running slowly.
.. seealso::
.. seealso::
...
@@ -598,55 +591,35 @@ which incur interpreter overhead.
...
@@ -598,55 +591,35 @@ which incur interpreter overhead.
.. testcode::
.. testcode::
def take(n, seq):
def take(n, iterable):
return list(islice(seq, n))
"Return first n items of the iterable as a list"
return list(islice(iterable, n))
def enumerate(iterable):
def enumerate(iterable
, start=0
):
return izip(count(), iterable)
return izip(count(
start
), iterable)
def tabulate(function):
def tabulate(function
, start=0
):
"Return function(0), function(1), ..."
"Return function(0), function(1), ..."
return imap(function, count())
return imap(function, count(start))
def iteritems(mapping):
return izip(mapping.iterkeys(), mapping.itervalues())
def nth(iterable, n):
def nth(iterable, n):
"Returns the nth item or raise StopIteration"
"Returns the nth item or empty list"
return islice(iterable, n, None).next()
return list(islice(iterable, n, n+1))
def all(seq, pred=None):
def quantify(iterable, pred=bool):
"Returns True if pred(x) is true for every element in the iterable"
"Count how many times the predicate is true"
for elem in ifilterfalse(pred, seq):
return sum(imap(pred, iterable))
return False
return True
def padnone(iterable):
def any(seq, pred=None):
"Returns True if pred(x) is true for at least one element in the iterable"
for elem in ifilter(pred, seq):
return True
return False
def no(seq, pred=None):
"Returns True if pred(x) is false for every element in the iterable"
for elem in ifilter(pred, seq):
return False
return True
def quantify(seq, pred=None):
"Count how many times the predicate is true in the sequence"
return sum(imap(pred, seq))
def padnone(seq):
"""Returns the sequence elements and then returns None indefinitely.
"""Returns the sequence elements and then returns None indefinitely.
Useful for emulating the behavior of the built-in map() function.
Useful for emulating the behavior of the built-in map() function.
"""
"""
return chain(
seq
, repeat(None))
return chain(
iterable
, repeat(None))
def ncycles(
seq
, n):
def ncycles(
iterable
, n):
"Returns the sequence elements n times"
"Returns the sequence elements n times"
return chain.from_iterable(repeat(
seq
, n))
return chain.from_iterable(repeat(
iterable
, n))
def dotproduct(vec1, vec2):
def dotproduct(vec1, vec2):
return sum(imap(operator.mul, vec1, vec2))
return sum(imap(operator.mul, vec1, vec2))
...
...
Lib/test/test_itertools.py
Dosyayı görüntüle @
f1f46f03
...
@@ -1185,52 +1185,32 @@ Samuele
...
@@ -1185,52 +1185,32 @@ Samuele
[22]
[22]
[25, 26, 27, 28]
[25, 26, 27, 28]
>>> def take(n, seq):
>>> def take(n, iterable):
... return list(islice(seq, n))
... "Return first n items of the iterable as a list"
... return list(islice(iterable, n))
>>> def enumerate(iterable):
>>> def enumerate(iterable
, start=0
):
... return izip(count(), iterable)
... return izip(count(
start
), iterable)
>>> def tabulate(function):
>>> def tabulate(function
, start=0
):
... "Return function(0), function(1), ..."
... "Return function(0), function(1), ..."
... return imap(function, count())
... return imap(function, count(start))
>>> def iteritems(mapping):
... return izip(mapping.iterkeys(), mapping.itervalues())
>>> def nth(iterable, n):
>>> def nth(iterable, n):
... "Returns the nth item"
... "Returns the nth item
or empty list
"
... return list(islice(iterable, n, n+1))
... return list(islice(iterable, n, n+1))
>>> def all(seq, pred=None):
>>> def quantify(iterable, pred=bool):
... "Returns True if pred(x) is true for every element in the iterable"
... "Count how many times the predicate is true"
... for elem in ifilterfalse(pred, seq):
... return sum(imap(pred, iterable))
... return False
... return True
>>> def padnone(iterable):
>>> def any(seq, pred=None):
... "Returns True if pred(x) is true for at least one element in the iterable"
... for elem in ifilter(pred, seq):
... return True
... return False
>>> def no(seq, pred=None):
... "Returns True if pred(x) is false for every element in the iterable"
... for elem in ifilter(pred, seq):
... return False
... return True
>>> def quantify(seq, pred=None):
... "Count how many times the predicate is true in the sequence"
... return sum(imap(pred, seq))
>>> def padnone(seq):
... "Returns the sequence elements and then returns None indefinitely"
... "Returns the sequence elements and then returns None indefinitely"
... return chain(
seq
, repeat(None))
... return chain(
iterable
, repeat(None))
>>> def ncycles(
seq
, n):
>>> def ncycles(
iterable
, n):
... "Returns the sequence elements n times"
... "Returns the seq
e
uence elements n times"
... return chain(*repeat(
seq
, n))
... return chain(*repeat(
iterable
, n))
>>> def dotproduct(vec1, vec2):
>>> def dotproduct(vec1, vec2):
... return sum(imap(operator.mul, vec1, vec2))
... return sum(imap(operator.mul, vec1, vec2))
...
@@ -1315,24 +1295,6 @@ perform as purported.
...
@@ -1315,24 +1295,6 @@ perform as purported.
>>> nth('abcde', 3)
>>> nth('abcde', 3)
['d']
['d']
>>> all([2, 4, 6, 8], lambda x: x
%2
==0)
True
>>> all([2, 3, 6, 8], lambda x: x
%2
==0)
False
>>> any([2, 4, 6, 8], lambda x: x
%2
==0)
True
>>> any([1, 3, 5, 9], lambda x: x
%2
==0,)
False
>>> no([1, 3, 5, 9], lambda x: x
%2
==0)
True
>>> no([1, 2, 5, 9], lambda x: x
%2
==0)
False
>>> quantify(xrange(99), lambda x: x
%2
==0)
>>> quantify(xrange(99), lambda x: x
%2
==0)
50
50
...
...
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