Die Python-Hash-Tabellenimplementierung löst beim Löschen manchmal „ValueError: too much Values ​​to Unpack“ ausPython

Python-Programme
Anonymous
 Die Python-Hash-Tabellenimplementierung löst beim Löschen manchmal „ValueError: too much Values ​​to Unpack“ aus

Post by Anonymous »

Ich implementiere eine benutzerdefinierte Hash-Tabelle in Python mit offener Adressierung und Tombstones für gelöschte Einträge.
Das Problem: Das Löschen eines Schlüssels funktioniert manchmal einwandfrei, löst aber gelegentlich
ValueError: too much Values ​​to unpack (expected 2) – unvorhersehbar aus.
Das ist meine Hash-Funktion:

Code: Select all

def get_valid_index(data_list, key):
result = 0
for i in key:
result += ord(i)
idx = result % len(data_list)
first_tombstone = None
sidx = idx

while True:
kv = data_list[idx]
if kv == 'tombstone' and first_tombstone is None:
first_tombstone = idx
elif kv is None:
if first_tombstone is not None:
return first_tombstone
else:
return idx
elif kv is not None:
k, v = kv   #  0.7:
self.resize(len(self.data) * 2)
return
k, v = kv
if k == key:
self.data[idx] = (key, value)

def __getitem__(self, key):
idx = get_valid_index(self.data, key)
kv = self.data[idx]
if kv is None or kv == 'tombstone':
return None
else:
k, v = kv
if k == key:
return v

def delete(self, key):
idx = get_valid_index(self.data, key)
kv = self.data[idx]
if kv is None or kv == 'tombstone':
print('It is None already')
return None
else:
self.data[idx] = 'tombstone'
self.count -= 1
if self.count  8:
self.resize(len(self.data)//2)

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post