Dictionaries in python, like HashMap in java, exist and operate in the form of key-value pairs. Its characteristics are as follows.
Access by key, not offset;
Key-value pairs are out of order;
Keys and values can be arbitrary objects;
Variable length and arbitrary nesting;
In a dictionary, there can be no sequential operation. Although dictionaries are similar to lists in some ways, don't put lists on them.
Click (here) to collapse or open.
# Encoding: utf-8
#! /usr/bin/python
# File name: map.py
Table = {'abc': 1,' def':2,' ghi':3}
print a form
# Dictionary inversion
map=dict([(v,k) for k,v in table.iteritems()])
# Dictionary traversal
For keys in map.keys ():
Print keywords, ":",mapping [keywords]
Print lens (map)
Print map.keys ()
Print map.values ()
# dictionary addition, deletion, modification and search
# It is necessary to mention here that for the expansion of dictionaries, only a new key-value pair needs to be defined.
# For lists, you can only use the append method or assign values by segments.
Map [4]="xyz "
Print map
Del map [4]
Print map
Map [3]= "Update"
Print map
if map.has_key( 1):
Print "1 key input"
{'abc': 1,' ghi': 3,' def': 2}
1 : abc
2 : def
3 : ghi
three
[ 1, 2, 3]
['abc ',' def ',' ghi']
{ 1: 'abc ',2: 'def ',3: 'ghi ',4: 'xyz'}
{ 1: 'abc ',2: 'def ',3: 'ghi'}
{ 1: 'abc ',2: 'def ',3: 'update'}
1 type