Python defaultdict - Collections module : Python programming for beginners

Publicado el: 29 septiembre 2022
en el canal de: Brendan Callaghan
45
2

Python defaultdict - Collections module : Python programming for beginners

from collections import defaultdict

pets_counter = {"dogs": 1, "cats": 1}
print(pets_counter)
print(pets_counter["dogs"])
print(pets_counter["birds"])
print(pets_counter.get("birds"))
print(pets_counter.setdefault("birds",1))
print(pets_counter)
print(pets_counter.setdefault("dogs",2))
print(pets_counter)

print('*' * 100)

pets_counter = defaultdict(int)
print(pets_counter)
pets_counter["dogs"]
print(pets_counter)
pets_counter["cats"] = "bob"
print(pets_counter)

print('*' * 100)

pets = [
("dog", "Affenpinscher"),
("dog", "Terrier"),
("dog", "Boxer"),
("cat", "Abyssinian"),
("cat", "Birman"),
]

group = defaultdict(list)
print(group)

for name, breed in pets:
group[name].append(breed)

for name, breed in group.items():
print(name, '--', breed)

group["birds"]
print(group)

group["dog"].append("bulldog")
print(group)


En esta página del sitio puede ver el video en línea Python defaultdict - Collections module : Python programming for beginners de Duración hora minuto segunda en buena calidad , que subió el usuario Brendan Callaghan 29 septiembre 2022, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 45 veces y le gustó 2 a los espectadores. Disfruta viendo!