Dictionaries
the dictionary in Python, like the HashMap in Java, exists and operates in the form of key-value pairs, which are characterized by access through keys, not offsets; key-value pairs are unordered; keys and values can be arbitrary; length variable, arbitrary nested; In the dictionary, no more sequence operations, although the dictionary is similar to the list in some ways, but do not set the list in the dictionary
1 Basic Operation python code strong>>>> table = {' abc ':1, ' def ':2, ' Ghi ': 3} >>> table[' abc '] 1 >>> len (table) 3 >>> Table.keys () [' abc ', ' Ghi ', ' def '] >>> table.values () [1, 3, 2] >>> table.has_key (' def ') true > >> table.items () [(' abc ',  1), (' Ghi ', 3), (' Def ', 2)]
2) Modify, delete, add python code >>> table = {' abc ': 1, ' Def ': 2, ' Ghi ': 3} >>> t able[' ghi '] = (' g ', ' h ', ' I ') >>> table {' abc ': 1, ' Ghi ': (' g ', ' h ', ' I '), ' Def ': 2} >>> del ta ble[' abc '] >>> table {' Ghi ': (' g ', ' h ', ' I '), ' Def ': 2} >>> table[' xyz '] = [' x ', ' y ', ' Z '] & gt;>> Table {' xyz ': [' x ', ' y ', ' z '], ' ghi ': (' g ', ' h ', ' I '), ' Def ': 2}
here, for the dictionary expansion, just define a new key-value pair, and for the list, you can only use the Append method or fragment assignment.
3) Traversal of the dictionary python code >>> table = {' abc ':1, ' def ':2, ' Ghi ': 3} >>> for key in table.keys (): print key, '/t ', table[key] abc 1 ghi 3 def 2