Python uses the zipper method to implement the dictionary method example, python zipper

Source: Internet
Author: User

Python uses the zipper method to implement the dictionary method example, python zipper

Preface

A dictionary is also called a discrete list. The greatest feature is that it uses keys to find its corresponding values. Its time complexity is O (1 ), the following article introduces how to implement the dictionary using the zipper method in python.

In Python, how does one use a list to implement a dictionary?

The biggest problem with dictionary implementation by list is to solve the hash conflict. What should I do if the same location is obtained by calculating different keys in the list?

The simplest way is to use the zipper method.

Zipper: adds another list to each position in a list, so that a hash conflict can be stored. When the selected hash function is good enough,

The number of num is large enough to ensure that each list in the list contains only one element. The location of the element calculated based on the key.

Time to O (1.

Example

Class MyDict: def _ init _ (self, num = 100): # specify the list size self. _ num = num self. _ lst = [] for _ in range (self. _ num): self. _ lst. append ([]) def update (self, key, value): # Add key-value key_index = hash (key) % self. _ num for I, (k, v) in enumerate (self. _ lst [key_index]): if key = k: self. _ lst [key_index] [I] = [key, value] break else: self. _ lst [key_index]. append ([key, value]) def get (self, key): # pop-up value key_index = hash (key) % self based on the specified key. _ num for k, v in self. _ lst [key_index]: if k = key: return v else: raise KeyError ('no such {} key '. format (key) def pop (self, key): # based on the key pop-up element and delete key_index = hash (key) % self. _ num for I, (k, v) in enumerate (self. _ lst [key_index]): if k = key: result = v self. _ lst. pop (I) return result else: raise KeyError ('no such {} key '. format (key) def _ getitem _ (self, key): # You can use a subscript to set key_index = hash (key) % self. _ num for k, v in self. _ lst [key_index]: if k = key: return v else: raise KeyError ('no such {} key '. format (key) def keys (self): # obtain all keys for index in range (self. _ num): for k, v in self. _ lst [index]: yield k def values (self): # obtain all values for index in range (self. _ num): for k, v in self. _ lst [index]: yield v def items (self): # retrieve all entries for index in range (self. _ num): for item in self. _ lst [index]: yield item

Time found by key, visible

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Related Article

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.