python dictionary of commonly used methods on

Python dictionary can store any type of objects, such as strings, numbers, tuples ...... advantages: value convenience, speed

1, create a dictionary

The dictionary by a key (key) and the corresponding value (value) consisting of pairs. Dictionary also referred associative array or hash table. The basic syntax is as follows:

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

Note:
Each key and value separated by a colon (:), each pair with a comma, each pair separated by commas, the whole in curly braces ({}).
Key must be unique, but the value is not necessary.
Value may take any data type, but it must be immutable, such as strings, arrays, or tuples.

2, access to the dictionary values

print (d [ 'key']) If the value does not exist in the write will be error

print (d.get ( 'key')) If a value is written does not exist, the process returns None

print (d.get ( 'key', 'not find')) if no value exists, return "not find"

print (d.keys ()) # Get all key value 
print (d.values ()) # value of all values acquired
if 'key' in d: # determines whether there is key
Print ( 'key')
for K, V in d.items (): # traversing dictionary
print (k, v)
Into a list format without conversion, retaining the original characteristics of the dictionary 

for k in dict:
Print (k, dict [k])
Print (k, dict.get (k)) value of speed, good performance
 

3, modify the dictionary

d [ 'key'] = value key modifications exist i.e., key does not exist i.e. new 
d.setdefault ( 'key', value) can only add

4, delete dictionary elements
d.pop ( 'key') values must be passed, since dictionaries are unordered 
d.popitem () Delete a random
del D [ 'Key']
d.clear () empty dictionary

5, Other
print (dict.items ()) outputs a list format (list on non-true sense) 
Print (list (dict.items ())) The dictionary key value and turn into a multidimensional list

 Output:

 

len ( dict ):计算字典元素个数,即键的总数。
str ( dict ):输出字典可打印的字符串。
type (variable):返回输入的变量类型,如果变量是字典就返回字典类型。
6, dictionary keys must be initialized
If you view the access key does not exist in a dictionary, it will lead to a keyerror. When traveling keyerror, the program will crash due to the runtime error
With "in" check membership
IF 'bananas were' in Fruits: 
Fruits [ 'bananas were'] + = 1
the else:
Fruits [ 'bananas were'] = 1
See "bananas" key is in the dictionary, because do not have this key, so its value is initialized to 1 so that you can eliminate the possibility of travel keyerror
with "not in" replace "in"
IF 'bananas were' Not in Fruits: 
Fruits [ 'bananas were'] + = 0 # initialization, if desired
fruits [ 'bananas'] + = 1
using the method setdefault
Letter in Word for: 
IF Letter in Vowels:
found.setdefault (Letter, 0)
found [Letter] + =. 1
setDefault can ensure that the key does not exist for initializing a specified a default value, or do nothing (i.e. said associated value of existing bonds will remain the same)

before accessing a key, keyerror can be avoided by ensuring that each key has an associated value dictionary. Although this in and not in operators can help, but the technology is more mature method using setdault

Guess you like

Origin www.cnblogs.com/zhaoqing-cao/p/11621331.html