Python's collection Series-Ordered dictionary (ordereddict)

Source: Internet
Author: User

Orderddict is a supplement to the dictionary type, and he remembers the order in which the dictionary elements were added

Note: The dictionary default loop only outputs key

1 ImportCollections2DIC =collections. Ordereddict ()3dic["K1"] ="v1"4dic["K2"] ="v2"5dic["K3"] ="v3"6 Print(DIC)7 #the principle of implementation: equivalent to a list (ordered) to maintain the dictionary (unordered) ordering, the following is only for understanding8 #dic = {"K1": "V1", "K2": "V2"}9 #li = ["K1", "K2"]Ten #For I in Li: One #Print (Dic.get (i)) A  - execution Result: no matter how many times the result is executed -Ordereddict ([('K1','v1'), ('K2','v2'), ('K3','v3')])
1 def Popitem (self, last=True):2         "Od.popitem () (k, V), return and Remove a (key, value) pair. 3         Pairs is returned in LIFO order if last is true or FIFO order if False.
1 #ordered Delete and specify delete2 ImportCollections3DIC =collections. Ordereddict ()4dic["K1"] ="v1"5dic["K2"] ="v2"6dic["K3"] ="v3"7 Print(DIC)8Dic.popitem ()#In order to take off, each take off the last, equivalent to the memory stack storage, LIFO principle, and pop () is forced to take out the specified value9 Print(DIC)Ten  One Execution Result: AOrdereddict ([('K1','v1'), ('K2','v2'), ('K3','v3')]) -Ordereddict ([('K1','v1'), ('K2','v2')])
1 def move_to_end (self, key, last=True):2         "move an existing element to the End (or beginning if last==false). 3 4         raises keyerror If the element does not exist. 5 When         last=true, acts like a fast version of Self[key]=self.pop (key).
1 #moves the specified key value to the last2 ImportCollections3DIC =collections. Ordereddict ()4dic["K1"] ="v1"5dic["K2"] ="v2"6dic["K3"] ="v3"7 Print(DIC)8Dic.move_to_end ("K1")#moves the specified key value to the last9 Print(DIC)Ten  One #Execution Result: AOrdereddict ([('K1','v1'), ('K2','v2'), ('K3','v3')]) -Ordereddict ([('K2','v2'), ('K3','v3'), ('K1','v1')])
 1   def  SetDefault (self, key, Default=none):  2   "   " Span style= "color: #008080;" >3  if  key in   self:  4  return   Self[key]  5  Self[key] = default  Span style= "color: #008080;" >6  return  default 
1 #Add default Keys2 ImportCollections3DIC =collections. Ordereddict ()4dic["K1"] ="v1"5dic["K2"] ="v2"6dic["K3"] ="v3"7 Print(DIC)8Dic.setdefault ("K4","V4")#The default key value is None, but you can add a value9 Print(DIC)Ten  One #Execution Result: AOrdereddict ([('K1','v1'), ('K2','v2'), ('K3','v3')]) -Ordereddict ([('K1','v1'), ('K2','v2'), ('K3','v3'), ('K4','V4')])

Python's collection Series-Ordered dictionary (ordereddict)

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.