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 438: Ordenar una lista de tuplas por el valor de punto flotante de cada tupla. | |
productos = [('Mouse', '29.5'), ('Teclado', '77.9'), ('Auriculares', '15.9'), ('Deademas', '49.9')] | |
print(productos) | |
resultado = sorted(productos, key=lambda p: float(p[1])) | |
print(resultado) |
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 437: Remover las tuplas vacías desde una lista de tuplas. | |
tuplas = [(1, 2), (5,), (), (9, 8, 7), (1,), (), ()] | |
print(tuplas) | |
print(len(tuplas)) | |
print() | |
tuplas = [t for t in tuplas if t] |
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 436: Reemplazar el último valor de cada tupla de una lista. | |
tuplas = [(10, 20, 30), (40, 50, 60), (70, 80, 90)] | |
print(tuplas) | |
tuplas = [t[0:-1] + (1000,) for t in tuplas] | |
print(tuplas) |
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 435: Imprimir una tupla con el método de instancia format(). | |
numeros = (2, 3, 5, 7) | |
print('El contenido de la tupla es: {}'.format(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 434: Crear un objeto diccionario a partir de una lista de objetos tupla. | |
lista_tuplas = [('a', 1), ('b', 2), ('a', 2), ('b', 3), ('c', 1), ('e', 5), ('c', 2), ('b', 1), ('b', 5)] | |
d = {} | |
for x, y in lista_tuplas: | |
d.setdefault(x, []).append(y) | |
print(d) |
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 433: Invertir el orden de los elementos de un objeto tupla. | |
numeros = (1, 2, 3, 4, 5) | |
print(numeros) | |
resultado = tuple(reversed(numeros)) | |
print(resultado) |
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 432: Crear una lista de tuplas a partir de la función zip(). | |
lista = [(1, 2), (3, 4), (5, 6), (7, 8)] | |
resultado = list(zip(*lista)) | |
print(resultado) |
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 431: Convertir un objeto tupla en un objeto diccionario. | |
lenguajes = (('Python', '3.8.1'), ('JavaScript', 'ES6'), ('C#', '7.0'), ('PHP', '7.1.2')) | |
print(lenguajes) | |
lenguajes_diccionario = dict((l, v) for l, v in lenguajes) | |
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 430: Computar la longitud de un objeto tupla con la función len(). | |
numeros = (2, 3, 5, 7, 11, 13, 17, 19) | |
print(numeros) | |
print(len(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 429: Encontrar el índice de un elemento en una tupla. | |
lenguajes = ('Python', 'C#', 'JavaScript', 'C++', 'PHP') | |
indice = lenguajes.index('JavaScript') | |
print(indice) | |
try: | |
indice = lenguajes.index('php') |