我是一年级计算机科学专业的学生。我正在尝试自学哈希表以进行面试。在阅读了几篇关于它们的文章后,我想看看我是否得到它的最好方法是在 Python 中实现我自己的哈希表。这就是我所做的。请有人看看它,让我知道你的想法吗?我是否正确理解了我要使用哈希表做什么?
storage_array = []
def show_menu():
menu_option = int(raw_input("Enter 1 to store data, Enter 2 to retrieve data: "))
if (menu_option == 1):
store_data()
elif (menu_option == 2):
retrieve_data()
def store_data():
key_for_data = raw_input("Please enter the key for the data you want to store: ")
actual_data = raw_input("Please enter the data you want to store: ")
ascii_count = generate_hash(key_for_data)
print ascii_count
storage_array[ascii_count] = actual_data
print "The data:'", actual_data, "'has been stored at index:'", ascii_count, "' which is the ascii count of:'", key_for_data, "'"
show_menu()
def retrieve_data():
key_for_data = raw_input("Enter the key for the data you want to retrieve: ")
ascii_count = generate_hash(key_for_data)
if (storage_array[ascii_count] == None):
print "No data was stored under this key"
else:
print "The data you requested for key:'", key_for_data, "'with ASCII count:'", ascii_count, "' is:'", storage_array[ascii_count], "'"
show_menu()
def generate_hash(input):
character_list = list(input)
ascii_count = 0
for character_index in range(0,len(character_list)):
ascii_count += ord(character_list[character_index])
return ascii_count
def initiate_list():
for repeat_index in range(0,1000):
storage_array.append(None)
print "List initiated with index's to 1000"
initiate_list()
show_menu()
##Or is it meant to hash the key like a dictionary and then store
##the value for that key in the hashed value in the hash table?