Advertisement
elcocodrilotito

(incompleto) Clase diccionario

Apr 26th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. class Diccionario:
  2.     def __init__(self): #Falta por ajustar unas cosas
  3.         #Por ejemplo, que sea agrande la lista, o en vez de agrandarse,
  4.         #que al index obtenido mediante hash() se le aplique un resto
  5.         #para que quepa en la lista
  6.         self.lista=[[(0,0)] for i in range(10000)]
  7.  
  8.     def __setitem__(self,clave,valor):
  9.         esta=False
  10.         for i in range(len(self.lista[hash(clave)%10000])):
  11.             if clave==self.lista[hash(clave)%10000][i][1]:
  12.                 esta=True
  13.                 x=i
  14.         if esta:
  15.             self.lista[hash(clave)%10000][x]=valor,clave
  16.         else:
  17.             self.lista[hash(clave)%10000].append((valor,clave))
  18.  
  19.     def __getitem__(self,clave):
  20.         for i in range(len(self.lista[hash(clave)%10000])):
  21.             if clave==self.lista[hash(clave)%10000][i][1]:
  22.                 return self.lista[hash(clave)%10000][i][0]
  23.  
  24.     def eliminar(self,clave):
  25.         for i in range(len(self.lista[hash(clave)])):
  26.             if clave==self.lista[hash(clave)][i][1]:
  27.                 del self.lista[hash(clave)][i]
  28.  
  29.     def pop(self,clave):
  30.         for i in range(len(self.lista[hash(clave)])):
  31.             if clave==self.lista[hash(clave)][i][1]:
  32.                 x=self.lista[hash(clave)][i][0]
  33.                 del self.lista[hash(clave)][i]
  34.                 return x
  35.  
  36.     def __contains__(self,clave):
  37.         for i in range(len(self.lista[hash(clave)])):
  38.             if clave==self.lista[hash(clave)][i][1]:
  39.                 return True
  40.         return False
  41.  
  42.     def __iter__(self):
  43.         pass
  44. diccionario1=Diccionario()
  45. Diccionario.__setitem__(diccionario1,"hola","adrian")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement