1. Dictionaries
1.1 The difference between a dictionary type and a sequence type:
Data is accessed and accessed in different ways.
The sequence type is indexed only by numeric type keys (numerically sequentially from the beginning of the sequence);
A mapping type can be used as a key for other object types (such as numbers, strings, ganso, usually strings as keys), and the keys of the sequence type are different, and the key of the mapping type is directly or indirectly associated with the stored data value.
The data in the mapping type is unordered. This is not the same as the sequence type, and the sequence type is numerically ordered.
The mapping type is directly "mapped" to a value with a key.
1.2 Dictionary creation:
Method ①:>>> Dict1 = {} >>> dict2 = {' name ': ' Earth ', ' Port ': +} >>> Dict1, Dict2 ({}, {' Port ': +, ' name ': ' Earth '})
Method ②: From Python version 2.2 >>> fdict = Dict (([' X ', 1], [' Y ', 2]) >>> fdict {' Y ': 2, ' X ': 1}
Method ③: From the Python version 2.3, you can create a "default" dictionary with a convenient built-in method Fromkeys (), with the same value for the elements in the dictionary (none by default if not given):>>> ddict = {}.fromkeys ((' x ', ' Y '),-1) >>> ddict {' y ':-1, ' X ':-1} >>> >>> edict = {}.fromkeys (' foo ', ' Bar ') >& Gt;> edict {' foo ': None, ' Bar ': none}
1.3 Basic features and operation:
Key type: Can be any immutable type;
Auto Add: Key does not exist or can be assigned a value, new item is created automatically
Membership: In lookup is a key
1). Accessing the values in the dictionary
Adict[key] Returns the value of key key, if key is not in the dictionary, a keyerror is raised.
2). Check if key is in the dictionary
A, Has_key () method shape: Adict.haskey (' name ') has –>true, no –>false
b, in, No in shape: ' Name ' in adict with –>true, no –>false
3). length
Len (d) Returns the number of items in D (key-value pairs);
4). Update the Dictionary
A, add a data item (new Element) or key-value pair
Adict[new_key] = value form adds an item
B. Update a data item (element) or key-value pair
Adict[old_key] = New_value
C. Delete a data item (element) or key-value pair
Del Adict[key] Delete key key item/del adict delete entire dictionary
Adict.pop (key) deletes the key keys and returns the value corresponding to key
1.4 Dictionary Methods
1>, Adict.keys () returns a list containing all keys for the dictionary;
2>, Adict.values () returns a list containing all the value of the dictionary;
3>, Adict.items () returns a list that contains all (key, value) Ganso;
4>, Adict.clear () deletes all items or elements in the dictionary;
1>>> x={}2>>> y=x3>>> x['Key']='value'4>>>y5{'Key':'value'}6>>> x={}7>>>y8{'Key':'value'}9>>>Ten>>> One>>> x={} A>>> y=x ->>> x['Key']='value' ->>>y the{'Key':'value'} ->>>x.clear () ->>>y -{}
View Code
5>, Adict.copy () returns a copy of a shallow copy of the dictionary;
6>, Adict.fromkeys (seq, val=none) creates and returns a new dictionary that is the key to the dictionary with the elements in the SEQ, and Val does the initial value corresponding to all the keys in the dictionary (default is None);
7>, Adict.get (key, default = None) returns the value of key in the dictionary, and returns the value of default if the key does not exist in the dictionary (default defaults to none);
8>, Adict.has_key (key) returns True if key is in the dictionary, otherwise false. Now with in, not in;
9>, Adict.iteritems (), Adict.iterkeys (), adict.itervalues () are the same as the non-iterative methods they correspond to, but they return an iteration instead of a list;
10>, Adict.pop (Key[,default]) are similar to the Get methods. If there is a key in the dictionary, delete and return the key corresponding to the Vuale; if the key does not exist and the default value is not given, the keyerror exception is thrown;
11>, Adict.setdefault (Key, Default=none) and set () methods are similar, but if the key key is not present in the dictionary, it is assigned by adict[key] = default;
12>, Adict.update (bdict) adds dictionary bdict key-value pairs to the dictionary adict.
Python Basic Tutorial Summary 3-Dictionary