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
b66d33fc
Kaydet (Commit)
b66d33fc
authored
Şub 19, 2007
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Provide an example of defaultdict with non-zero constant factory function.
üst
1bff7969
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
8 deletions
+12
-8
libcollections.tex
Doc/lib/libcollections.tex
+12
-8
No files found.
Doc/lib/libcollections.tex
Dosyayı görüntüle @
b66d33fc
...
...
@@ -311,16 +311,20 @@ languages):
When a letter is first encountered, it is missing from the mapping, so the
\member
{
default
_
factory
}
function calls
\function
{
int()
}
to supply a default
count of zero. The increment operation then builds up the count for each
letter. This technique makes counting simpler and faster than an equivalent
technique using
\method
{
dict.get()
}
:
letter.
\begin{verbatim}
>>> d =
{}
>>> for k in s:
d[k] = d.get(k, 0) + 1
The function
\function
{
int()
}
which always returns zero is just a special
case of constant functions. A faster and more flexible way to create
constant functions is to use
\function
{
itertools.repeat()
}
which can supply
any constant value (not just zero):
>>> d.items()
[('i', 4), ('p', 2), ('s', 4), ('m', 1)]
\begin{verbatim}
>>> def constant
_
factory(value):
... return itertools.repeat(value).next
>>> d = defaultdict(constant
_
factory('<missing>'))
>>> d.update(name='John', action='ran')
>>> '
%(name)s %(action)s to %(object)s' % d
'John ran to <missing>'
\end{verbatim}
Setting the
\member
{
default
_
factory
}
to
\class
{
set
}
makes 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