Python built-in dictionary: dict support, Dict full name dictionary, also known as map in other languages, using key-value (Key-value) storage, with extremely fast search speed
Set is similar to Dict and is a set of keys, but does not store value. Because key cannot be duplicated, there is no duplicate key in set.
Dictionary = {' name ': ' Bob ', ' age ': ' City ': ' Hangzhou '}dictionary[' city '] ' Hangzhou '
In addition to the values that are placed during initialization, you can also put
>>> dictionary = {"name": "Bob", "age": A, "City": "Hangzhou"}>>> print dictionary{' city ': ' Hangzhou ', ' Age ': ' The ' name ': ' Bob '}>>> dictionary[' score ']= 90>>> print dictionary{' city ': ' Hangzhou ', ' age ': 12 , ' score ': +, ' name ': ' Bob '}
Since a key can only correspond to one value, the value is placed multiple times for a key, and the following values will flush the previous value out:
>>> dictionary["Score"]= 100>>> dictionary["age"]= 30>>> print dictionary{' city ': ' Hangzhou ', ' age ': +, ' score ': +, ' name ': ' Bob '}
If key does not exist in Dict, it will be an error
>>> dictionary["Age"]30>>> dictionary["ages"]traceback (most recent call last): File "<stdin > ", Line 1, in <module>keyerror: ' Ages '
To avoid a key that does not exist, there are two ways to avoid errors, one is to in
determine if a key exists:
>>> ' age ' in dictionarytrue>>> ' ages ' in Dictionaryfalse
The second is the Get method provided by Dict, if key does not exist, you can return none, or the value you specified:
>>> dictionary.get ("Age") #get () method to get key is the age of Value30 >>> dictionary.get ("Ages", "ages not Exist ") #key不存在, return the value you have set for ' ages not exist ' >>> dictionary.get (" ages ")
Elements that are duplicated in set are automatically filtered out
>>> s = set ([1,2,2,3,3,4,5]) >>> sset ([1, 2, 3, 4, 5])
You can add a delete element to a set by using Add (key) and remove (key)
>>> S.add ("Test") >>> sset ([1, 2, 3, 4, 5, ' Test ']) >>> s.remove (4) >>> sset ([1, 2, 3, 5, ' Test '])
Python Basics-Dict and set