When I first learned python, I thought that the dictionary was unordered, and by multiple insertions, such as Di = {}, multiple di[' TestKey ']= ' testvalue ' test to prove unordered.
Later came into contact with the dictionary search efficiency this thing, checked, the original dictionary in Python is in the order of the hash table to do some testing, such as Di = {1:1,3:3,2:2,4:4,5:5}, regardless of how the order of key-value pairs, print di will always {1:1, 2:2, 3:3, 4:4, 5:5}. So it seems that when inserting di[' key ']= ' value ', this set of key pairs sometimes does not go to the last position.
So it can also be said that the dictionary is orderly.
Of course, the concept of order and disorder here depends on where you are easy to understand, from the data structure it is the order of the hash table, but from the display layer it does not from left to right by the assignment of the added.
In addition, the value of the Python dictionary, regardless of length, its time/space complexity is O (1).
Leetcode list Two values add equal to a number, return two values corresponding to the title of the index, if you want to use a single-layer loop to achieve, just use the hash table.
https://leetcode.com/problems/two-sum/
Implementation code:
Class solution (Object):
def twosum (self, Nums, target):
"""
: Type Nums:list[int]
: Type Target:int
: Rtype:list[int]
"""
dict={}
For i,x in Enumerate (nums):
If Dict.get (target-x,none) = = None:
Dict[nums[i]] = i
Else
Return (Dict[target-x],i)
Faster than a double-decker cycle speed.
Python dictionary ordered unordered and search efficiency, hash table