Common dictionary constants and actions
Operation |
Explain |
D = {} |
Empty dictionary |
D = {' spam ': 2, ' Egg ': 3} |
Two-item dictionaries |
D = {' food ': {' Ham ': 1, ' Egg ': 2}} |
Nesting |
D = Dict.fromkeys ([' A ', ' B ']) |
Other construction techniques |
D = dict (Zip (keylist, valslist)) |
Key, corresponding pair, key list |
D = Dict (name= ' Bob ', age=42) |
|
d[' eggs '] |
Index operations with Keys |
' Eggs ' in D |
Membership: Key exists test |
D.keys () |
Method: Key |
D.values () |
Value |
D.items () |
Key + value |
D.copy () |
Copy |
D.get (key, default) |
Default |
D.update (D2)
|
Merge |
D.pop (Key) |
Delete, etc. |
Len (D) |
Length |
D[key] = 42 |
New, modifier keys |
Del D[key] |
Delete entries by key |
List (D.keys ())
|
Dictionary View |
D = {x:x*2 for x in range (10)} |
Dictionary parsing (Python3.0) |
This article is from the "Missed Footsteps" blog, please be sure to keep this source http://xxmspace.blog.51cto.com/1056016/1763169
Python Basics four, dictionaries