3次元配列でMLE、defaultdictでTLEと苦戦しましたが、行ごとに使い回す2次元配列に圧縮してAC。メモリと速度のバランスを取る過程が学びになりました🎁
https://gist.github.com/maehrm/238def5fd85ab6a6a7043277189d1215
#AtCoder #競技プログラミング
3次元配列でMLE、defaultdictでTLEと苦戦しましたが、行ごとに使い回す2次元配列に圧縮してAC。メモリと速度のバランスを取る過程が学びになりました🎁
https://gist.github.com/maehrm/238def5fd85ab6a6a7043277189d1215
#AtCoder #競技プログラミング
UnionFindでグループを管理し、各リーダーにクラス人数をdefaultdictで保持。マージテク(小さい方を大きい方に統合)で計算量を抑えました🏫
https://gist.github.com/maehrm/b45bc22869475a70fb1fbf57e5500158
#AtCoder #競技プログラミング
UnionFindでグループを管理し、各リーダーにクラス人数をdefaultdictで保持。マージテク(小さい方を大きい方に統合)で計算量を抑えました🏫
https://gist.github.com/maehrm/b45bc22869475a70fb1fbf57e5500158
#AtCoder #競技プログラミング
Read more 👉 pym.dev/default-dict...
#Python
Read more 👉 pym.dev/default-dict...
#Python
from collections import defaultdict
places = defaultdict(list)
while True:
city, country = input('Place: ').split()
places[country].append(city) # list is guaranteed
print(places)
from collections import defaultdict
places = defaultdict(list)
while True:
city, country = input('Place: ').split()
places[country].append(city) # list is guaranteed
print(places)
defaultdict(float) is a dictionary that automatically starts any new key at 0.0
#Python #PythonTips #100DaysofCode
defaultdict(float) is a dictionary that automatically starts any new key at 0.0
#Python #PythonTips #100DaysofCode
¡Agrupa con collections.defaultdict!
# ✅ MÉTODO PYTHONIC (Limpio y directo con defaultdict ✨)
Código más limpio: Eliminas ramas condicionales innecesarias dentro de tus bucles.
🎥 www.youtube.com/@josecodetech/
#Programacion
¡Agrupa con collections.defaultdict!
# ✅ MÉTODO PYTHONIC (Limpio y directo con defaultdict ✨)
Código más limpio: Eliminas ramas condicionales innecesarias dentro de tus bucles.
🎥 www.youtube.com/@josecodetech/
#Programacion
これはPythonですが少し話題に上がっていたので。
これはPythonですが少し話題に上がっていたので。
from collections import defaultdict as dd
d = dd(lambda: dd(lambda: dd(list)))
from collections import defaultdict as dd
d = dd(lambda: dd(lambda: dd(list)))
¡Protege y simplifica tu código con defaultdict! 🚀
Más trucos para escribir código a prueba de balas aquí 🎥 www.youtube.com/@josecodetech/ #JoseCodeTech #Programacion #AprendePython #PythonTips #DefaultDict #CleanCode #Pythonic #DataScience
¡Protege y simplifica tu código con defaultdict! 🚀
Más trucos para escribir código a prueba de balas aquí 🎥 www.youtube.com/@josecodetech/ #JoseCodeTech #Programacion #AprendePython #PythonTips #DefaultDict #CleanCode #Pythonic #DataScience
my_dict.get(k, d): get value or default, once
my_dict.setdefault(k, d): set value if not set
dict.fromkeys(keys, d): make new dict with known keys
Counter(items): mapping that counts occurrences
defaultdict(list): mapping that defaults key lookups
my_dict.get(k, d): get value or default, once
my_dict.setdefault(k, d): set value if not set
dict.fromkeys(keys, d): make new dict with known keys
Counter(items): mapping that counts occurrences
defaultdict(list): mapping that defaults key lookups
1. get
2. setdefault
3. fromkeys
4. dict comprehension
5. collections.defaultdict
6. collections.Counter
You shouldn't always reach for defaultdict. Start with the simplest approach that works well for your situation.
#Python
1. get
2. setdefault
3. fromkeys
4. dict comprehension
5. collections.defaultdict
6. collections.Counter
You shouldn't always reach for defaultdict. Start with the simplest approach that works well for your situation.
#Python
Counter, defaultdict, and other dictionary-like objects also tend to support both of those syntaxes.
Counter, defaultdict, and other dictionary-like objects also tend to support both of those syntaxes.
d = defaultdict(0) # ❌ Error!
Pass a function that returns the default:
d = defaultdict(int) # int() returns 0
d = defaultdict(list) # list() returns []
d = defaultdict(dict) # dict() returns {}
d = defaultdict(0) # ❌ Error!
Pass a function that returns the default:
d = defaultdict(int) # int() returns 0
d = defaultdict(list) # list() returns []
d = defaultdict(dict) # dict() returns {}
One often-overlooked tool is `ChainMap`:
One often-overlooked tool is `ChainMap`:
don’t forget about `defaultdict`
don’t use a mutable object as a default value for a parameter
use `heapq` instead of constantly sorting
don’t forget about `defaultdict`
don’t use a mutable object as a default value for a parameter
use `heapq` instead of constantly sorting
github.com/marcodelmast...
github.com/marcodelmast...
сегодня я бы ненавидел его за мучения с github.com/pydantic/pyd...
сегодня я бы ненавидел его за мучения с github.com/pydantic/pyd...