Repository navigation
Conversation
Co-Authored-By: BethanyG <BethanyG@users.noreply.github.com>
also update contributor information in `config.json`
|
|
||
| for word in words: | ||
| word = word.strip(IGNORE) | ||
| if word: |
There was a problem hiding this comment.
..I sorta don't want t explain the difference here between setdefault() and get() in prose here. And then again, I sorta do. Especially since the "intro snippet" uses dict.get() (and maybe shouldn't).
Thoughts?
There was a problem hiding this comment.
I think it's a good idea to explain the difference here. When I was newer to Python, the difference between them was rather confusing.
I think one way of setting it up could be to show how the following two snippets would both achieve the same effect but in slightly different ways:
word_list[new_word] = word_list.get(new_word, 0) + 1word_list.setdefault(new_word, 0)
word_list[new_word] += 1There was a problem hiding this comment.
I like that. We might also want to point out in prose how each is evaluated, I think. get() is slow, but really clear. More info in this piece of the noted SO post: https://stackoverflow.com/a/22326645. And an if/else construct as well. Dunno if it is worth doing the long-hand equivalent, or just covering it in its own approach:
word_list[word] + 1 if word in word_list else word_list[word] = 1There is also using defaultdict.
| ``` | ||
|
|
||
| This approach avoids splitting the phrase into a word list by iterating over each _character_ in the sentence to determine if it belongs in a valid word (_only letters and numbers are allowed_). | ||
| As each valid word is built up, it is added to the dictionary. |
There was a problem hiding this comment.
Not sure how to phrase it, but we probably need to acknowledge that this strategy is ASCII dependent. The minute you move to a different locale in Unicode, you have to account for diacritical marks, and where they belong in a "word" (and what a "word" is in different languages).... and Python doesn't have a built-in module for that. So a 3rd party lib is needed (or a lot of processing code). Other strategies here are more flexible, since the punctuation and split() behaviors vary by the language locale setting.
|
|
||
| ## Which approach to use? | ||
|
|
||
| The most performant of these approaches is __ for longer text, and __ for smaller text. Memory-wise, ___ has the least overhead. |
There was a problem hiding this comment.
Guess we need timings or opinions here. 😄
|
@Yrahcaz7 - not a really serious, in-depth review - mostly just stream-of-consciousness comments as I read through. I do like your |
Co-authored-by: BethanyG <BethanyG@users.noreply.github.com>
also clarify "String Replace with Dictionary" -> "String Replace with Mutable Dictionary"
A revival of PR #3371.
Current changes:
filter()withcollections.Counter()" approachLeft to do:
dict.get()ordict.setdefault()".