Today, I'm going to write something every week to keep track of my study, mark.
The dictionary is the most flexible built-in data structure in Python, dictionaries are unordered collections (the order of the dictionaries is random for fast key lookups), Python uses the most optimized hashing algorithm to find keys; Dictionaries are similar to maps in Java, but there are not many types of maps in Java (HashMap , TreeMap, etc.).
Common dictionary Operations
Assign value
D = {} # empty dictionary d = {"name": "Zhangsan", "Age": # 2 Items Dictionary d = {"Fruit": {"Apple": 2, "Orange": 1}} # nested d = di Ct.fromkeys (["A", "B"], 2) # {' A ': 2, ' B ': 2}d = Dict (["Name", "Age"], ["Zhangsan", ' "[]]") # {' Age ': ' + ', ' n ' Ame ': ' Zhangsan '}
Take value
Name = d["name"]age = D.get ("Age", 0) # If the age attribute is not present, the value is 0keys = D.keys () # gets the key values = D.values () # get Valueif ' n Ame ' in D: print ' name was in d '
Delete
D.pop (' sex ') # delete del d[' name ' according to key # Delete according to key
Other operations
D_1 = {"Sex": "Male"}d.update (d_1) # Merge Dict