Uh oh!
There was an error while loading. Please reload this page.
Refactor word_occurrence to use dict - #15186
Closed
Miladkhoshdel wants to merge 2 commits into
Closed
Conversation
Miladkhoshdelforce-pushed
the
refactor/word-occurrence
branch
from
September 4, 2026 18:28
e4e1612 to
2538821Comparefor more information, see https://pre-commit.ci
Miladkhoshdelforce-pushed
the
refactor/word-occurrence
branch
from
September 4, 2026 18:31
a673a04 to
5bccc57Comparecclauss
reviewed
Sep 4, 2026
| for word in sentence.split(): | ||
| occurrence[word] += 1 | ||
| occurrence[word] = occurrence.get(word, 0) + 1 |
Member
There was a problem hiding this comment.
Python's default_dict is in the standard library to accelerate use cases like this one. There is a function call overhead in the proposed solution that does not exist in the current solution.
Create a https://docs.python.org/3/library/timeit.html#timeit.timeit benchmark and compare the two on a big dataset.
ContributorAuthor
There was a problem hiding this comment.
Thanks for the feedback. I’ve kept the existing defaultdict implementation unchanged and moved the type-hint and typo fixes into a separate, focused PR: #15194.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
Refactor word_occurrence() to use a standard dict instead of defaultdict.
This change:
Checklist: