dictionary{}

1. What is the dictionary
dict in {} represent each separated by a comma, with the internal elements key:.. Value of the form to hold the data
{ "jj": "JJ", "jay": "Jay"}

Query efficiency is very high, the key element to locate
the internal key used to calculate a memory address (temporarily), the hash algorithm. Key must be immutable data type (hash key must be data type)
key data { key: value} Uniqueness

keys must be immutable hash data types can be used as a dictionary key

value without any limitation

2. Dictionary of CRUD
new 1. dictionary:

dic [key] = value 

dic.setdefault (key, value) do nothing if the key exists in the dictionary, or is added

through key queries do not have this key return None
# DIC = {} 
# # Zheng Xu: Lost on Journey 
# DIC [ 'Zheng Xu'] = "Lost on Journey" # directly entered stored data key to 
# DIC [ 'Huang Bo'] = "Crazy Stone " 
# dic [" Wang Baoqiang "] =" A world without thieves " 
# dic [" Wang Baoqiang "] =" shock troops "# If a key already exists. It will replace the original value, modify 
# 
# dic.setdefault ( "Anthony") 
# dic.setdefault ( "Anthony", "Infernal Affairs") # If there is a key, will not perform the new 
# 
# Print (dic)

  


2. 删除
pop(key)
popitem()
clear()
del dict[key]

dic = { "Felix Wong": "Dragon", "Jackie Lui": "Swordsman", "Alec": "Dragon", "age of six small children": "Journey"} 
# dic.pop ( " Jackie Lui ") # delete the specified key 
# dic.popitem () # random delete 
# del dic [" Felix Wong "] # delete 
# dic.clear () # empty dictionary 

print (dic)

  


3. Modify
dic [key] = new value
Update ()
4. query
1. Query key directly dict [key]
2. GET (key, if the data returned key does not exist)
3. setDefault () 1. Add performed process. 2. results
The operation

keys      acquired presence of a high copy list of all key 
values obtained for all the values present in a high copy list
items list to obtain a high copy all key exists in the form of already Ganso

Deconstruction, or directly to the data tuples in the list out.
A, B, C = (A1, B1, C1) must correspond

dic = { "Liu can": "Musgrave", "Zhao Si": "Xiaoguang", "Wang Musheng": "Fan Wei", "thank Feet": "in Yuet Sin Canossian", "Li Taiguo": "Home"} 
# DIC [ 'Wang Musheng'] = "Liu" 
DIC2 = { "Liu can": "sun brother", "Zhao Si": "GitHub", "Wang Musheng": "Feng Wang", "thank Feet": "von Timo "," Wang take ":" Golden boss "} 
dic.update (DIC2) 
Print (dic) 

# query 
dic = { 'Liu Neng': 'big brother Yang', 'Zhao Si': 'github', 'Wang Mu-sheng': 'Wang Feng', 'Xie Bigfoot': 'von Timo', 'Lida Guo': 'kid', 'Wang to take': 'gold boss'} 
# 1. The most intuitive. Directly with key 
Print (DIC [ 'Jay']) # When this key does not exist will be given 
# 2. get Method 
print (dic.get ( "thank Feet", "Jay's not here." 
)) # No key. Back None # 3. setDefault () 1. Add (look there is no key, if it had, if not, performing new) 2. key according to the value returned 
DIC = {} 
DIC [ "Galen"] = "Demacian force" 
value = dic.setdefault ( "Fiona", "no swords Kyi") # add 
value2 = dic.setdefault ( "Galen", "Liu") # due to the existing key. So new is not performed. Direct results

  

6. traversing dictionary
for Key in dict:
dict [Key]

for k, v in dict.items():
k , v

dic = { "Wang Feng": "Music for half of the continent," "Jay": "Asian Music King", "Luo": "Lord of the Dance in Asia"} 

# dictionary traversal 
# print (dic.keys ()) # dict_keys ( [ 'Wang Feng', 'Jay', 'Luo']) but not as a list of list 
# for Key in dic.keys (): 
# Print (Key) to get the # Key 
# Print (dic [Key]) to get value # 

Print # (dic.values ()) 
# for value in dic.values (): 
# Print (value) 


# can traverse the dictionary 
# [( 'Wang Feng', 'music for half of the continent'), ( 'Jay', 'Asia '), music King (' Luo ',' Asian Lord of the dance ')] 
# Print (dic.items ()) # got the key and value 
for k, v in dic.items (): when you need to traverse the dictionary # . when it comes to key value in operation and 
    print (k) # tuples 
    Print (V) 


# dictionary itself is an iterator object may be for directly recycled 
for el in dic:# Get directly Key 
    Print (EL) 
    Print (DIC [EL]) 

consistent with the number and # Arguments behind the front of the number of packets
# A, b = (10, 20) # deconstruction, unpacking 
# Print (A) 
# Print (B)

  

7. dictionary nested.

wf = {
    "name": "汪峰",
    "age": 48,
    "成名曲": "春天里",
    "wife": {
        "name": "章子怡",
        "age": 39,
        "工作": "演员"
    },
    "children":[
        {"num": "001", "name": "汪一", "hobby": "唱歌"},
        {"num": "002", "name": "汪二", "hobby": "演戏"} # wf['children'][1]['name']
    ]
}

# wf['wife']['age'] = wf['wife']['age'] + 10
# print(wf)

  

Guess you like

Origin www.cnblogs.com/forever-fortunate/p/11693998.html