Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

hashmap python

my_dict={'Dave' : '001' , 'Ava': '002' , 'Joe': '003'}
print(my_dict.keys())
print(my_dict.values())
print(my_dict.get('Dave'))
Comment

Code Example of Hashmap in Python

class HashTable:

	# Create empty bucket list of given size
	def __init__(self, size):
		self.size = size
		self.hash_table = self.create_buckets()

	def create_buckets(self):
		return [[] for _ in range(self.size)]

	# Insert values into hash map
	def set_val(self, key, val):
		
		# Get the index from the key
		# using hash function
		hashed_key = hash(key) % self.size
		
		# Get the bucket corresponding to index
		bucket = self.hash_table[hashed_key]

		found_key = False
		for index, record in enumerate(bucket):
			record_key, record_val = record
			
			# check if the bucket has same key as
			# the key to be inserted
			if record_key == key:
				found_key = True
				break

		# If the bucket has same key as the key to be inserted,
		# Update the key value
		# Otherwise append the new key-value pair to the bucket
		if found_key:
			bucket[index] = (key, val)
		else:
			bucket.append((key, val))

	# Return searched value with specific key
	def get_val(self, key):
		
		# Get the index from the key using
		# hash function
		hashed_key = hash(key) % self.size
		
		# Get the bucket corresponding to index
		bucket = self.hash_table[hashed_key]

		found_key = False
		for index, record in enumerate(bucket):
			record_key, record_val = record
			
			# check if the bucket has same key as
			# the key being searched
			if record_key == key:
				found_key = True
				break

		# If the bucket has same key as the key being searched,
		# Return the value found
		# Otherwise indicate there was no record found
		if found_key:
			return record_val
		else:
			return "No record found"

	# Remove a value with specific key
	def delete_val(self, key):
		
		# Get the index from the key using
		# hash function
		hashed_key = hash(key) % self.size
		
		# Get the bucket corresponding to index
		bucket = self.hash_table[hashed_key]

		found_key = False
		for index, record in enumerate(bucket):
			record_key, record_val = record
			
			# check if the bucket has same key as
			# the key to be deleted
			if record_key == key:
				found_key = True
				break
		if found_key:
			bucket.pop(index)
		return

	# To print the items of hash map
	def __str__(self):
		return "".join(str(item) for item in self.hash_table)


hash_table = HashTable(50)

# insert some values
hash_table.set_val('softhunt.net', 'some value')
print(hash_table)
print()

hash_table.set_val('website@example.com', 'some other value')
print(hash_table)
print()

# search/access a record with key
print(hash_table.get_val('softhunt.net.com'))
print()

# delete or remove a value
hash_table.delete_val('softhunt.net.com')
print(hash_table)
Comment

PREVIOUS NEXT
Code Example
Python :: enumerate count 
Python :: first flask api 
Python :: print(((x//y)+1)*z) means in python 
Python :: python is not operator 
Python :: zufälliger wert aus liste python 
Python :: linear plot 1D vector for x python 
Python :: find the index of nanmax 
Python :: checking time in time range 
Python :: python first letter to capitalize 
Python :: python using boolean len 
Python :: Random parola uretme 
Python :: beautifulsoup documentation 
Python :: how to save the command result with ! in python 
Python :: the best ide for python 
Python :: how to preserve white space when joining an array python 
Python :: is dictreader scoped in python 
Python :: tqdm continues afer break 
Python :: RuntimeError: input must have 3 dimensions, got 4 site:stackoverflow.com 
Python :: use ipython magic in script 
Python :: print greeting in python explication 
Python :: pandas get data from upper row 
Python :: right click vs left click pygame 
Python :: Python Global variable and Local variable with same name 
Python :: Python Pipelining Generators 
Python :: no repetir elementos en una lista python 
Python :: Python send sms curl 
Python :: dataframe to DatasetDict 
Python :: dict get keys tcl 
Python :: stacked percentage bar chart 
Python :: scatter plot python color according to gender 
ADD CONTENT
Topic
Content
Source link
Name
9+8 =