Python Hashing (Hash tables and hashlib)
bogotobogo.com site search:
Hash Tables
When we talk about hash tables, we're actually talking about dictionary. While an array can be used to construct hash tables, array indexes its elements using integers. However, if we want to store data and use keys other than integer, such as 'string', we may want to use dictionary.
Dictionaries in Python are implemented using hash tables. It is an array whose indexes are obtained using a hash function on the keys.
We declare an empty dictionary like this:
>>> D = {}
Then, we can add its elements:
>>> D['a'] = 1 >>> D['b'] = 2 >>> D['c'] = 3 >>> D {'a': 1, 'c': 3, 'b': 2}
It's a structure with (key, value) pair:
D[key] = value
The string used to "index" the hash table D is called the "key". To access the data stored in the table, we need to know the key:
>>> D['b'] 2
How we loop through the hash table?
>>> for k in D.keys(): ... print D[k] ... 1 3 2
If we want to print the (key, value) pair:
>>> for k,v in D.items(): ... print k,':',v ... a : 1 c : 3 b : 2
bogotobogo.com site search:
Hashing from two arrays
Using two Arrays of equal length, create a Hash object where the elements from one array (the keys) are associated with the elements of the other (the values):
>>> keys = ['a', 'b', 'c'] >>> values = [1, 2, 3] >>> hash = {k:v for k, v in zip(keys, values)} >>> hash {'a': 1, 'c': 3, 'b': 2}
Hashing
Here are some hashing samples using built-in hash function:
>>> map(hash, [0, 1, 2, 3]) [0, 1, 2, 3] >>> map(hash, ['0','1','2','3']) [6144018481, 6272018864, 6400019251, 6528019634] >>> hash('0') 6144018481
As we can see from the example, Python is using different hash() function depending on the type of data.
Python provides hashlib for secure hashes and message digests:
md5(), sha*():
>>> import hashlib >>> hashlib.md5('a')>>> hashlib.md5('a').digest() '\x0c\xc1u\xb9\xc0\xf1\xb6\xa81\xc3\x99\xe2iw&a' >>> hashlib.md5('a').hexdigest() '0cc175b9c0f1b6a831c399e269772661' >>> hashlib.sha512('a') >>> hashlib.sha512('a').digest() '\x1f@\xfc\x92\xda$\x16\x94u\ty\xeel\xf5\x82\xf2\xd5\xd7\xd2\x8e\x183]\xe0Z\xbcT\xd0V\x0e\x0fS\x02\x86\x0ce+\xf0\x8dV\x02R\xaa^t!\x05F\xf3i\xfb\xbb\xce\x8c\x12\xcf\xc7\x95{&R\xfe\x9au' >>> hashlib.sha512('a').hexdigest() '1f40fc92da241694750979ee6cf582f2d5d7d28e18335de05abc54d0560e0f5302860c652bf08d560252aa5e74210546f369fbbbce8c12cfc7957b2652fe9a75' >>>
Hashing example code
The following code is a revision from Sets (union/intersection) and itertools - Jaccard coefficient & shingling to check plagiarism. In this section, we used 64 bit integer (hash value from hash()) for the comparison of shingles instead of directly working on the string.
from __future__ import division
import itertools
import re
import hashlib
# a shingle in this code is a string with K-words
K = 4
def jaccard_set(s1, s2):
" takes two sets and returns Jaccard coefficient"
u = s1.union(s2)
i = s1.intersection(s2)
return len(i)/len(u)
def make_a_set_of_tokens(doc):
"""makes a set of K-tokens"""
# replace non-alphanumeric char with a space, and then split
tokens = re.sub("[^\w]", " ", doc).split()
sh = set()
for i in range(len(tokens)-K):
t = tokens[i]
for x in tokens[i+1:i+K]:
t += ' ' + x
sh.add(t)
return sh
if __name__ == '__main__':
documents = ["The legal system is made up of civil courts, criminal courts and specialty courts such as family law courts and bankruptcy court. Each court has its own jurisdiction, which refers to the cases that the court is allowed to hear. In some instances, a case can only be heard in one type of court. For example, a bankruptcy case must be heard in a bankruptcy court. In other instances, there may be several potential courts with jurisdiction. For example, a federal criminal court and a state criminal court would each have jurisdiction over a crime that is a federal drug offense but that is also an offense on the state level.",
"The legal system is comprised of criminal and civil courts and specialty courts like bankruptcy and family law courts. Every one of the courts is vested with its own jurisdiction. Jurisdiction means the types of cases each court is permitted to rule on. Sometimes, only one type of court can hear a particular case. For instance, bankruptcy cases an be ruled on only in bankruptcy court. In other situations, it is possible for more than one court to have jurisdiction. For instance, both a state and federal criminal court could have authority over a criminal case that is illegal under federal and state drug laws.",
"In many jurisdictions the judicial branch has the power to change laws through the process of judicial review. Courts with judicial review power may annul the laws and rules of the state when it finds them incompatible with a higher norm, such as primary legislation, the provisions of the constitution or international law. Judges constitute a critical force for interpretation and implementation of a constitution, thus de facto in common law countries creating the body of constitutional law."]
shingles = []
# handle documents one by one
# makes a list of sets which are compresized of a list of K words string
for doc in documents:
# makes a set of tokens
# sh = set([' ', ..., ' '])
sh = make_a_set_of_tokens(doc)
print("sh = %s") %(sh)
# hasing
bucket = map(hash, sh)
# print("bucket = %s") %(bucket)
# shingles : list of sets (sh)
shingles.append(set(bucket))
#print("shingles=%s") %(shingles)
combinations = list( itertools.combinations([x for x in range(len(shingles))], 2) )
print("combinations=%s") %(combinations)
# compare each pair in combinations tuple of shingles
for c in combinations:
i1 = c[0]
i2 = c[1]
jac = jaccard_set(shingles[i1], shingles[i2])
print("%s : jaccard=%s") %(c,jac)
Output is exactly the same as the one we got using string comparison:
combinations=[(0, 1), (0, 2), (1, 2)] (0, 1) : jaccard=0.0196078431373 (0, 2) : jaccard=0.0 (1, 2) : jaccard=0.0
List of Python Tutorials
- Python Home
- Introduction
- Running Python Programs (os, sys, import)
- Modules and IDLE (Import, Reload, exec)
- Object Types - Numbers, Strings, and None
- Strings - Escape Sequence, Raw String, and Slicing
- Strings - Methods
- Formatting Strings - expressions & method calls
- Files and os.path
- Traversing directories recursively
- Subprocess Module
- Regular Expressions with Python
- Object Types - Lists
- Object Types - Dictionaries and Tuples
- Functions def, *args, **kargs
- Functions lambda
- Built-in Functions
- map, filter, and reduce
- Decorators
- List Comprehension
- Sets (union/intersection) and itertools - Jaccard coefficient & shingling to check plagiarism
- Hashing (Hash tables & hashlib)
- Dictionary Comprehension with zip
- The yield keyword
- Generator Functions and Expressions
- generator.send() method
- Iterators
- Iterators II
- Classes and Instances (__init__, __call__, etc.)
- if__name__ == '__main__'
- argparse
- @static method vs class method
- Private attributes and private methods
- bits, bytes, bitstring, and constBitStream
- Python Object Serialization - pickle and json
- Python Object Serialization - yaml and json
- Priority queue and heap queue data structure
- Graph data structure
- Dijkstra's shortest path algorithm
- Prim's spanning tree algorithm
- Closure
- Functional programming in Python
- Remote running a local file using ssh
- SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table
- SQLite 3 - B. Selecting, updating and deleting data
- MongoDB with PyMongo I - Installing MongoDB ...
- Python HTTP Web Services - urllib, httplib2
- Web scraping with Selenium for checking domain availability
- Multithreading ...
- Python Network Programming I - Basic Server / Client : A Basics
- Python Network Programming I - Basic Server / Client : B File Transfer
- Python Network Programming II - Chat Server / Client
- Python Network Programming III - Echo Server using socketserver network framework
- Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn
- Python Interview Questions I
- Python Interview Questions II
- Python Interview Questions III
- Python & C++ with SIP
- PyDev with Eclipse
- Matplotlib
- NumPy array basics A
- NumPy Matrix and Linear Algebra
- Celluar Automata
- Batch gradient descent algorithm
- Longest Common Substring Algorithm
- Python Unit Test - TDD using unittest.TestCase class
- Simple tool - Google page ranking by keywords
- Google App Hello World
- Google App webapp2 & WSGI
- Uploading Google App Hello World
- Python 2 vs Python 3
- virtualenv and virtualenvwrapper
- Uploading a big file to AWS S3 using boto module
- Scheduled stopping and starting an AWS instance
- Cloudera CDH5 - Scheduled stopping and starting services
- Removing Cloud Files - Rackspace API with curl and subprocess
- Checking if a process is running/hanging and stop/run a scheduled task on Windows
- Apache Spark 1.3 with PySpark (Spark Python API) Shell
- Apache Spark 1.2 Streaming
- bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...
- Fabric - streamlining the use of SSH for application deployment
- Neural Networks with backpropagation for XOR using one hidden layer
- NLP - NLTK (Natural Language Toolkit) ...
- RabbitMQ(Message broker server) & Celery(Task queue) ...
- OpenCV3 & Matplotlib ...
- Simple tool - Concatenating slides using FFmpeg ...
- iPython - Signal Processing with NumPy
- iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github
- iPython and Jupyter Notebook with Embedded D3.js
- Downloading YouTube videos using youtube-dl embedded with Python
- Machine Learning : scikit-learn ...
- Django 1.6/1.8 Web Framework ...
Very nice blog helpful to everyone python interview questions
ReplyDeletemanual testing interview questions
software testing interview questions
mvc interview questions
ReplyDeleteBest content.Thank you so much for sharing,i have learnt something new.I hope you will share more information like this,keep updating.
Best Data Science Certification Course in Bangalore