Implement a hash table in python

Source: Internet
Author: User
This article introduces a hash table implemented in python. the conflict processing method is the open address method, the conflict expression is Hi (H (key) + 1) modm, and m indicates the table length. More difficult zipper implementation later The code is as follows:


#! /Usr/bin/env python
# Coding = UTF-8
# Implement a hash table (linear address and then hash)

Def ChangeKey (key, m, di ):
Key01 = (key + di) % m
Return key01

A = raw_input ("Please entry the numbers: \ n"). split ()
M = len ()
Dict01 = {}
For I in:
Key = int (I) % m
If "% s" % key in dict01:
NewKey = ChangeKey (key, m, 1)
While "% s" % NewKey in dict01: # because the following dict01 key value is saved as a string, the string format is also used for judgment.
NewKey = ChangeKey (NewKey, m, 1)
Dict01 ["% s" % NewKey] = int (I)
Else:
Dict01 ["% s" % key] = int (I)
Print dict01

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.