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
fcb393c0
Kaydet (Commit)
fcb393c0
authored
Agu 09, 2011
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add support for unary plus and unary minus to collections.Counter()
üst
18205baf
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
1 deletion
+31
-1
collections.rst
Doc/library/collections.rst
+13
-1
__init__.py
Lib/collections/__init__.py
+11
-0
test_collections.py
Lib/test/test_collections.py
+5
-0
NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/collections.rst
Dosyayı görüntüle @
fcb393c0
...
...
@@ -264,7 +264,7 @@ Common patterns for working with :class:`Counter` objects::
c.items() # convert to a list of (elem, cnt) pairs
Counter(dict(list_of_pairs)) # convert from a list of (elem, cnt) pairs
c.most_common()[:-n:-1] # n least common elements
c += Counter()
# remove zero and negative counts
+c
# remove zero and negative counts
Several mathematical operations are provided for combining :class:`Counter`
objects to produce multisets (counters that have counts greater than zero).
...
...
@@ -284,6 +284,18 @@ counts, but the output will exclude results with counts of zero or less.
>>> c | d # union: max(c[x], d[x])
Counter({'a': 3, 'b': 2})
Unary addition and substraction are shortcuts for adding an empty counter
or subtracting from an empty counter.
>>> c = Counter(a=2, b=-4)
>>> +c
Counter({'a': 2})
>>> -c
Counter({'b': 4})
.. versionadded:: 3.3
Added support for unary plus and unary minus.
.. note::
Counters were primarily designed to work with positive integers to represent
...
...
Lib/collections/__init__.py
Dosyayı görüntüle @
fcb393c0
...
...
@@ -672,6 +672,17 @@ class Counter(dict):
result
[
elem
]
=
newcount
return
result
def
__pos__
(
self
):
'Adds an empty counter, effectively stripping negative and zero counts'
return
self
+
Counter
()
def
__neg__
(
self
):
'''Subtracts from an empty counter. Strips positive and zero counts,
and flips the sign on negative counts.
'''
return
Counter
()
-
self
########################################################################
### ChainMap (helper for configparser and string.Template)
...
...
Lib/test/test_collections.py
Dosyayı görüntüle @
fcb393c0
...
...
@@ -943,6 +943,11 @@ class TestCounter(unittest.TestCase):
c
.
subtract
(
'aaaabbcce'
)
self
.
assertEqual
(
c
,
Counter
(
a
=-
1
,
b
=
0
,
c
=-
1
,
d
=
1
,
e
=-
1
))
def
test_unary
(
self
):
c
=
Counter
(
a
=-
5
,
b
=
0
,
c
=
5
,
d
=
10
,
e
=
15
,
g
=
40
)
self
.
assertEqual
(
dict
(
+
c
),
dict
(
c
=
5
,
d
=
10
,
e
=
15
,
g
=
40
))
self
.
assertEqual
(
dict
(
-
c
),
dict
(
a
=
5
))
def
test_helper_function
(
self
):
# two paths, one for real dicts and one for other mappings
elems
=
list
(
'abracadabra'
)
...
...
Misc/NEWS
Dosyayı görüntüle @
fcb393c0
...
...
@@ -252,6 +252,8 @@ Library
- Issue #12540: Prevent zombie IDLE processes on Windows due to changes
in os.kill().
- Add support for unary plus and unary minus to collections.Counter().
- Issue #12683: urlparse updated to include svn as schemes that uses relative
paths. (svn from 1.5 onwards support relative path).
...
...
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