Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
944db38c
Commit
944db38c
authored
May 26, 2015
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #23509: Speed up Counter operators
(Based on patch by Serhiy Storchaka.)
parent
06f155f4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
2 deletions
+10
-2
Lib/collections/__init__.py
Lib/collections/__init__.py
+10
-2
No files found.
Lib/collections/__init__.py
View file @
944db38c
...
...
@@ -736,14 +736,22 @@ class Counter(dict):
def
__pos__
(
self
):
'Adds an empty counter, effectively stripping negative and zero counts'
return
self
+
Counter
()
result
=
Counter
()
for
elem
,
count
in
self
.
items
():
if
count
>
0
:
result
[
elem
]
=
count
return
result
def
__neg__
(
self
):
'''Subtracts from an empty counter. Strips positive and zero counts,
and flips the sign on negative counts.
'''
return
Counter
()
-
self
result
=
Counter
()
for
elem
,
count
in
self
.
items
():
if
count
<
0
:
result
[
elem
]
=
0
-
count
return
result
def
_keep_positive
(
self
):
'''Internal method to strip elements with a negative or zero count'''
...
...
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