Arranging and Searching Data
149
data = [22, 40, 102, 105, 23, 31, 6, 5]
hash_table = [None] * 15
tblLen = len(hash_table)
def hash_function(value, table_size):
return value % table_size
for value in data:
hash_table[hash_function(value, tblLen)] = value
print(hash_table)
[105, 31, None, None, None, 5, 6, 22, 23, None, 40, None,
102, None, None]
To find a particular value again, you just run it through
hash_function
. For
example,
print(hash_table[hash_function(102, tblLen)])
displays
102
as
output after locating its entry in
hash_table
. Because the hash values are unique
in this particular case,
hash_function
can locate the needed data every time.
Do'stlaringiz bilan baham: |