Disclaimer: Transfer from CSDN (http://blog.csdn.net/csujiangyu/article/details/45176399)
1. Create an empty dictionary
>>> dic = {}>>> type (DIC)'dict'>
2. Direct Assignment creation
>>> dic = {'spam'egg'bar' : 3}>>> dic{'bar'egg ' ' spam ': 1}
3. Create by keyword dict and keyword parameters
>>> dic = dict (spam = 1, egg = 2, bar =3)>>> dic{'bar' 'egg''spam': 1}
4. Create by two-tuple list
>>> list = [('spam', 1), ('egg', 2), (' Bar', 3)]>>> dic = dict (list)>>> dic{' Bar ' ' Egg ' ' spam ': 1}
5.dict and zip combine to create
>>> dic = dict (Zip ('abc', [1, 2, 3]))>>> dic{' a'c''b': 2}
6. Create by Dictionary derivation
for in range (3)}>>>1:2, 2:4}
7. Create through Dict.fromkeys ()
Usually used to initialize the dictionary, setting the value of the default
' x ' )>>>'x'x ' x'}
8. Other
>>> list = ['x'y'z', 3 ]>>> dic = dict (Zip (list[::2], list[1::2]) >>> dic{' y ' ' x ' ' Z ': 3}
Several ways that Python creates dict