Python using the Zipper method to implement the Dictionary method sample code

Source: Internet
Author: User
This article mainly introduces Python using the Zipper method to implement the dictionary, the text gives a detailed example code, I believe that we have a certain reference value, the need for friends can take a look below.

Objective

The dictionary is also called the hash list, the biggest feature is to find its corresponding value by key of its time complexity is O (1), the following article will introduce you to Python using the Zipper method to implement the dictionary.

How do I use a list to implement a dictionary in Python?

The biggest problem of using list to implement dictionary is resolving hash conflict, what should I do if I get the same position by calculating different keys in the list?

The easiest way is to use the zipper method.

Zipper method: is to add a list to each position in a list, so that there is a hash conflict can also be stored in, when the selected hash function is good enough,

The number of NUM is large enough to guarantee that there is only one element in each list in the list. Based on the location of the element where the key is calculated, then the value can be reached

To O (1) of the time.

Method Example


Class Mydict:def init (self, num=100): # Specifies the list size self._num = num SELF._LST = [] for _ in range (Self._num): self._lst.a Ppend ([]) 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): # Popup value according to the specified key 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 pop (self, key): # pops the element according to key and Deletes 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) RE Turn result else:raise keyerror (' No such {} key '. Format (key)) def getitem (self, key): # can be evaluated by subscript key_index = Hash (ke Y)% self._num for K, V in Self._lst[key_index]: if k = = Key:return v else:raise keyerror (' No such {} key '. Form at (key)) dEF keys: # Get all key for index in range (Self._num): for K, V in Self._lst[index]: Yield K def values: # Fetch All value for index in range (Self._num): to K, V in Self._lst[index]: Yield v def items: # Get all entries for Inde X in range (Self._num): For item in SELF._LST[INDEX]: Yield item

The time that is found by key is visible

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.