Implement the dictionary by zipper, and implement the dictionary by zipper

Source: Internet
Author: User

Implement the dictionary by zipper, and implement the dictionary by zipper

 

Dictionary:

It is also called a scattered list. The biggest feature is to use the key to find its corresponding value. Its time complexity is O (1 ).

 

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.

1 class MyDict: 2 def _ init _ (self, num = 100): # specify the list size 3 self. _ num = num 4 self. _ lst = [] 5 for _ in range (self. _ num): 6 self. _ lst. append ([]) 7 8 def update (self, key, value): # Add key-value 9 key_index = hash (key) % self. _ num10 for I, (k, v) in enumerate (self. _ lst [key_index]): 11 if key = k: 12 self. _ lst [key_index] [I] = [key, value] 13 break14 else: 15 self. _ lst [key_index]. append ([key, value]) 16 17 def get (self, key): # pop-up value 18 key_index = hash (key) % self based on the specified key. _ num19 for k, v in self. _ lst [key_index]: 20 if k = key: 21 return v22 else: 23 raise KeyError ('no such {} key '. format (key) 24 25 def pop (self, key): # Delete 26 key_index = hash (key) % self according to the key pop-up element. _ num27 for I, (k, v) in enumerate (self. _ lst [key_index]): 28 if k = key: 29 result = v30 self. _ lst. pop [self. _ num] (I) 31 return result32 else: 33 raise KeyError ('no such {} key '. format (key) 34 35 def _ getitem _ (self, key): # You can use a subscript to set the value to 36 key_index = hash (key) % self. _ num37 for k, v in self. _ lst [key_index]: 38 if k = key: 39 return v40 else: 41 raise KeyError ('no such {} key '. format (key) 42 43 def keys (self): # Get all key44 for index in range (self. _ num): 45 for k, v in self. _ lst [index]: 46 yield k47 48 def values (self): # obtain all value49 for index in range (self. _ num): 50 for k, v in self. _ lst [index]: 51 yield v52 53 def items (self): # retrieve all entries 54 for index in range (self. _ num): 55 for item in self. _ lst [index]: 56 yield item

Time found by key, visible

 

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.