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 :: how to run a python script in background windows 
Python :: python image crop 
Python :: how to type using selenium python 
Python :: scroll to element selenium python 
Python :: save model pytorch 
Python :: how to check if number is negative in python 
Python :: break python 
Python :: Simple Splash screen in pyqt5 
Python :: str replace pandas 
Python :: binary search python 
Python :: pandas order dataframe by index of other dataframe 
Python :: python re search print 
Python :: remove brases from array py 
Python :: plotly color specific color 
Python :: set points size in geopandas plot 
Python :: Hungry Chef codechef solution 
Python :: python square number 
Python :: decrypt vnc password 
Python :: create dictionary 
Python :: compare times python 
Python :: install os conda 
Python :: smtp python set subject 
Python :: how to make timer in python 
Python :: lambda function dataframe 
Python :: python sys.argv exception 
Python :: how to filter queryset with foreign key in django 
Python :: django model get field verbose name 
Python :: example of ternary operator in python 
Python :: how to change the disabled color in tkinter 
Python :: concat dataframe pandas 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =