Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created April 2, 2021 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/608a88c9d19bfa074d57419e11dfec8f to your computer and use it in GitHub Desktop.
Save parzibyte/608a88c9d19bfa074d57419e11dfec8f to your computer and use it in GitHub Desktop.
"""
https://parzibyte.me/blog
"""
class Juego:
def __init__(self, nombre, precio):
self.nombre = nombre
self.precio = precio
# Método que nos dice si un objeto es igual a otro
# https://parzibyte.me/blog/2021/04/01/comparar-objetos-python/
def __eq__(self, otro_juego):
return otro_juego.nombre == self.nombre and otro_juego.precio == self.precio
def __str__(self):
return f"{self.nombre}, {self.precio}"
def eliminar_duplicados(lista):
nueva_lista = []
for elemento in lista:
if not elemento in nueva_lista:
nueva_lista.append(elemento)
return nueva_lista
# Fíjate que Cuphead aparece 2 veces (con el mismo precio y nombre) y Hollow Knight también
lista = [Juego("Cuphead", 180), Juego("Cuphead", 180), Juego(
"Resident Evil 2", 430), Juego("Hollow Knight", 150), Juego("Cuphead", 180), Juego("Hollow Knight", 150)]
sin_duplicados = eliminar_duplicados(lista)
print("===\nOriginal: ===\n")
for elemento in lista:
print(str(elemento))
print("===\nSin repetidos===\n")
for elemento in sin_duplicados:
print(str(elemento))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment