When the index of a list or a tuple does not reach our goal, we think of another sequence, the dictionary.
Create
A dictionary consists of key-value pairs consisting of multiple keys and corresponding values.
Key is unique. Value can not be unique
phonebook = {‘xidada‘:‘110‘‘lizongli‘:‘112‘‘wangqishan‘:‘119‘}
dict function Creation Dictionary
Use the Dict function to create a dictionary of other mappings or sequences
>>>items = [(‘name‘‘xidada‘), (‘age‘64)]>>>d = dict(items)>>>d{‘age‘:64‘name‘:‘xidada‘}
Dict Creating dictionaries from keywords
‘xidada‘42)>>>d{‘age‘:64‘name‘:‘xidada‘}
Dictionary basic Operations
The
Change dictionary
adds new content to the dictionary by adding a new key/value pair. Change or delete an existing key/value pair such as the following instance:
Copy code code such as the following:
dict = { ' Name ' : Zara ' , ' age ' : 7 , ' class ' : ' first ' };d ict[ ' age ' ] = 8 ; # Update existing entry Dict[ ' School ' ] = "DPS School" ; # Add new entry print "dict[' age ']:" , Dict[ ' age ' ];p rint "dict[' School ']:" , Dict[ ' School ' ]; #dict [ ' age ' ]: 8 #dict [ ' School ' ]: DPS School
Delete a dictionary element
The ability to delete a single element also clears the dictionary. Emptying requires only one operation.
Show Delete a dictionary with the del command. Examples include the following:
Here write the code slice dict = {' Name ':' Zara ',' age ':7,' Class ':' first '};d el dict[' Name '];# Delete key is' Name 'Entry Dict.clear ();# Empty dictionary all Entries del dict;# Delete dictionary Print"dict[' age ']:", dict[' age '];p rint"dict[' School '):", dict[' School ']; #但这会引发一个异常. Because the dictionary no longer exists after using del: dict[' age ']:#Traceback(most recent):# File"test.py", line8, in <module># Print"dict[' age ']:", dict[' age '];#TypeError:' type 'Object is unsubscriptable
Properties of dictionary keys
A dictionary value can be taken without restrictions on any Python object, be it a standard object, or a user-defined one, but not a key.
Two important points to remember:
1) Do not agree with the same key appears two times. When created, it is assumed that the same key is assigned two times. The latter value is remembered, such as the following instance:
dict = {‘Name‘‘Zara‘‘Age‘7‘Name‘‘Manni‘"dict[‘Name‘]: ", dict[‘Name‘];#以上实例输出结果:#dict[‘Name‘]: Manni
2) The key must be immutable, so it can be used as a number, a string or a tuple, so the list is not. Examples include the following:
dict = {[‘Name‘‘Zara‘‘Age‘7"dict[‘Name‘]: ", dict[‘Name‘];#以上实例输出结果:#Traceback (most recent call last):# File "test.py", line 3, in <module># dict = {[‘Name‘]: ‘Zara‘, ‘Age‘: 7};#TypeError: list objects are unhashable
dictionary built-in functions & methods
The Python dictionary includes the following built-in functions:
1, CMP (Dict1, DICT2): compare two dictionary elements.
2. Len (dict): Calculates the number of dictionary elements. The total number of keys.
3, str (dict): output dictionary printable string representation.
4. Type (variable): Returns the type of the input variable, assuming that the variable is a dictionary and returns the dictionary type.
The Python dictionary includes the following built-in methods:
1, Radiansdict.clear (): Delete all elements in the dictionary
2, Radiansdict.copy (): Returns a shallow copy of a dictionary
3. Radiansdict.fromkeys (): Create a new dictionary with the key of the dictionary in sequence seq, Val is the initial value corresponding to all keys of the dictionary
4, Radiansdict.get (Key, Default=none): Returns the value of the specified key. Assumed value does not return the default value in the dictionary
5, Radiansdict.has_key (key): Assuming the key in the dictionary dict return True, otherwise return false
6, Radiansdict.items (): Returns a traversed (key, value) tuple array in a list
7, Radiansdict.keys (): Returns a dictionary of all keys in a list
8, Radiansdict.setdefault (Key, Default=none): Similar to get (), but assuming that the key does not already exist in the dictionary, the key will be added and the value will be set to default
9, Radiansdict.update (DICT2): The dictionary dict2 key/value pairs updated to Dict
10. Radiansdict.values (): Returns all values in the dictionary as a list
Python Basics-Dictionaries: When indexes are bad