Python Counter / Defaultdict / Dictionary

preview_player
Показать описание
Python Counter / Defaultdict / Dictionary
Рекомендации по теме
Комментарии
Автор

just because it's less code to write doesn't mean its faster

miramar-
Автор

We can upgrade first solution using get() dict method. It return second arg if key not in dict.keys()

NemoFish-me
Автор

I’ve been asked questions like this, where I had to count items in a list, during both phone screen and onsite interviews for Amazon. They didn’t allow me to use Counter, but they did let me use defaultdict. :)

AlperKaplan
Автор

How is his solution any faster? O compute complexity seems the same.

RobertLugg
Автор

So much code abstraction so much more memory usage. Just decode it yourself.

brutusunix
Автор

This actually simplifies a lot of leetcode problems where one needs a counter object

rodrigomorales
Автор

what if you want to filter out numbers and special characters?

pieordie
Автор

I can do fewer lines:
>>>get_counter = Counter
though I messed up the default value, so maybe not.

DrDeuteron
Автор

from collections import Counter

count = Counter('aba')

print(count)

tutex
Автор

aitutorialmaker AI fixes this. Python Counter Defaultdict Dictionary overview

RusulColee
Автор

What’s wrong with doing: length = len(string)

ADailyNerd
Автор

imagine thinking lines of code = speed...

Forien
Автор

it would be way more useful to learn how defaultdict or counter optimizes this pattern instead of just learning that there's a magic word to do it for you

Temporalus
Автор

Progression should be like,

1. if in counter: ....
*2. counter[c] = counter.get(c, 0) + 1* # Missing this
3. counter[c]: defaultdict(int) += 1
4. counter = Counter(s)

hello_there
Автор

counter[item] = 1 + counter.get(item, 0)