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
34c35b27
Kaydet (Commit)
34c35b27
authored
Nis 03, 2010
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add subtract() method to collections.Counter() objects.
üst
f70c5810
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
52 additions
and
0 deletions
+52
-0
collections.rst
Doc/library/collections.rst
+11
-0
collections.py
Lib/collections.py
+28
-0
test_collections.py
Lib/test/test_collections.py
+11
-0
NEWS
Misc/NEWS
+2
-0
No files found.
Doc/library/collections.rst
Dosyayı görüntüle @
34c35b27
...
...
@@ -230,6 +230,17 @@ For example::
>>> Counter('abracadabra').most_common(3)
[('a', 5), ('r', 2), ('b', 2)]
.. method:: subtract([iterable-or-mapping])
Elements are subtracted from an *iterable* or from another *mapping*
(or counter). Like :meth:`dict.update` but subtracts counts instead
of replacing them. Both inputs and outputs may be zero or negative.
>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> d = Counter(a=1, b=2, c=3, d=4)
>>> c.subtract(d)
Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})
The usual dictionary methods are available for :class:`Counter` objects
except for two which work differently for counters.
...
...
Lib/collections.py
Dosyayı görüntüle @
34c35b27
...
...
@@ -435,6 +435,34 @@ class Counter(dict):
if
kwds
:
self
.
update
(
kwds
)
def
subtract
(
self
,
iterable
=
None
,
**
kwds
):
'''Like dict.update() but subtracts counts instead of replacing them.
Counts can be reduced below zero. Both the inputs and outputs are
allowed to contain zero and negative counts.
Source can be an iterable, a dictionary, or another Counter instance.
>>> c = Counter('which')
>>> c.subtract('witch') # subtract elements from another iterable
>>> c.subtract(Counter('watch')) # subtract elements from another counter
>>> c['h'] # 2 in which, minus 1 in witch, minus 1 in watch
0
>>> c['w'] # 1 in which, minus 1 in witch, minus 1 in watch
-1
'''
if
iterable
is
not
None
:
if
isinstance
(
iterable
,
Mapping
):
self_get
=
self
.
get
for
elem
,
count
in
iterable
.
items
():
self
[
elem
]
=
self_get
(
elem
,
0
)
-
count
else
:
self_get
=
self
.
get
for
elem
in
iterable
:
self
[
elem
]
=
self_get
(
elem
,
0
)
-
1
if
kwds
:
self
.
subtract
(
kwds
)
def
copy
(
self
):
'Like dict.copy() but returns a Counter instance instead of a dict.'
return
Counter
(
self
)
...
...
Lib/test/test_collections.py
Dosyayı görüntüle @
34c35b27
...
...
@@ -669,6 +669,17 @@ class TestCounter(unittest.TestCase):
set_result
=
setop
(
set
(
p
.
elements
()),
set
(
q
.
elements
()))
self
.
assertEqual
(
counter_result
,
dict
.
fromkeys
(
set_result
,
1
))
def
test_subtract
(
self
):
c
=
Counter
(
a
=-
5
,
b
=
0
,
c
=
5
,
d
=
10
,
e
=
15
,
g
=
40
)
c
.
subtract
(
a
=
1
,
b
=
2
,
c
=-
3
,
d
=
10
,
e
=
20
,
f
=
30
,
h
=-
50
)
self
.
assertEqual
(
c
,
Counter
(
a
=-
6
,
b
=-
2
,
c
=
8
,
d
=
0
,
e
=-
5
,
f
=-
30
,
g
=
40
,
h
=
50
))
c
=
Counter
(
a
=-
5
,
b
=
0
,
c
=
5
,
d
=
10
,
e
=
15
,
g
=
40
)
c
.
subtract
(
Counter
(
a
=
1
,
b
=
2
,
c
=-
3
,
d
=
10
,
e
=
20
,
f
=
30
,
h
=-
50
))
self
.
assertEqual
(
c
,
Counter
(
a
=-
6
,
b
=-
2
,
c
=
8
,
d
=
0
,
e
=-
5
,
f
=-
30
,
g
=
40
,
h
=
50
))
c
=
Counter
(
'aaabbcd'
)
c
.
subtract
(
'aaaabbcce'
)
self
.
assertEqual
(
c
,
Counter
(
a
=-
1
,
b
=
0
,
c
=-
1
,
d
=
1
,
e
=-
1
))
class
TestOrderedDict
(
unittest
.
TestCase
):
def
test_init
(
self
):
...
...
Misc/NEWS
Dosyayı görüntüle @
34c35b27
...
...
@@ -47,6 +47,8 @@ Core and Builtins
Library
-------
- collections.Counter() now supports a subtract() method.
- Issue #8294: The Fraction constructor now accepts Decimal and float
instances directly.
...
...
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