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
# Ejercicio 407: Crear un Objeto Diccionario a partir de un Archivo JSON. | |
import json | |
with open('Parte001/programacion.json', 'r') as f: | |
programacion = json.load(f) | |
print(type(programacion)) | |
print(programacion) |
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
# Ejercicio 406: Guardar un objeto diccionario en un archivo JSON. | |
import json | |
programacion = {'lenguajes': [{'nombre': 'Python', 'version': '3.8.0'}, {'nombre': 'Java', 'version': '12'}, {'nombre': 'JavaScript', 'version': 'ES6'}, {'nombre': 'PHP', 'version': '7.1.2'}], 'ides': ['Visual Studio', 'Eclipse IDE', 'NetBeans IDE', 'PyCharm', 'PhpStorm']} | |
print(programacion) | |
print() |
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
# Ejercicio 405: Listar las llaves comunes entre dos objetos diccionario. | |
a = {'llave1': 1, 'llave2': 2, 'llave3': 3} | |
b = {'llave2': 1, 'llave4': 4, 'llave3': 3, 'llave5': 5, 'llave6': 6} | |
for k in set(a.keys()) & set(b.keys()): | |
print(k) |
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
# Ejercicio 404: Sumar la lista de valores de cada llave de un objeto diccionario. | |
numeros = {'a': [1, 2, 3], 'b': [9, 8, 7], 'c': [2, 3, 5]} | |
print(numeros) | |
print() | |
numeros = {k: sum(v) for k, v in numeros.items()} | |
print(numeros) |
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
# Ejercicio 403: Crear un diccionario con defaultdict usando dos listas de valores. | |
from collections import defaultdict | |
letras = ['A', 'B', 'C', 'D'] | |
numeros = [1, 2, 2, 3] | |
diccionario = defaultdict(set) | |
for l, n in zip(letras, numeros): |
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
# Ejercicio 402: Ordenar un diccionario a partir del valor de los elementos con Counter. | |
from collections import Counter | |
productos = {'Teclado': 57.9, 'Monitor': 299.9, 'Mouse': 29.5, 'Deademas': 73.5} | |
print(productos) | |
contador = Counter(productos) |
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
# Ejercicio 401: Contar la cantidad de elementos de los valores de un diccionario. | |
numeros = {'a': [1, 2, 3], 'b': [2, 3, 5, 7], 'c': [0, 1]} | |
cantidad = sum(map(len, numeros.values())) | |
print(cantidad) |
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
# Ejercicio 400: Comprobar si múltiples llaves existen en un diccionario. | |
persona = {'nombre': 'Edward', 'apellido': 'Ortiz', 'email': '[email protected]', 'edad': 33} | |
print(persona.keys() >= {'nombre', 'edad'}) | |
print(persona.keys() >= {'email', 'movil'}) | |
print(persona.keys() >= {'email', 'apellido', 'edad'}) | |
print(persona.keys() >= {'profesion'}) |
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
# Ejercicio 399: Imprimir el contenido de un diccionario con un ciclo for. | |
productos = {'id1': {'nombre': 'Teclado', 'precio': 63.9}, 'id2': {'nombre': 'Mouse', 'precio': 25.9}, 'id3': {'nombre': 'Deademas', 'precio': 203.9}, 'id4': {'nombre': 'Monitor', 'precio': 299.9}, 'id5': {'nombre': 'Smartphone', 'precio': 453}} | |
for p in productos: | |
print(p) | |
for c in productos[p]: | |
print('{}: {}'.format(c, productos[p][c])) | |
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
# Ejercicio 398: Usar la función enumerate() para iterar por los ítems de un diccionario. | |
diccionario = {'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e': 50} | |
for c, (k, v) in enumerate(diccionario.items(), 1): | |
print(k, v, c) |