字典
?
# 字典是python基本數據結構之一,相對于列表和元組,他是無序的,每次輸出都打亂了順序,沒有下標hello={110:{"name":"alex","age":28,"home":"shandong"},111:{"name":"ada","age":28,"home":"pk"},112:{"name":"simon","age":28,"home":"hz"},113:{"name":"kevin","age":28,"home":"gz"}
}print(hello.items())
hello2={110:{"id":"37232320000000"}
}# 增
# 相比于元組和列表,字典沒有append方法# 刪,字典沒有remove方法,只有pop 和del方法
pop = hello.pop(110)
print(pop)
# 返回值是被刪掉的值
del hello[112]
print("del 112 after:",hello)# 改hello[113][3]="he0llo"
print(hello[113])
hello[222]="mike"# 查
print(222 in hello) #返回True,經常被用于判斷在字典存在不存在一個鍵
print(hello.keys()) #以列表形式返回所有的的鍵,沒有值
print(hello.values()) #以列表返回所有字典的值,沒有鍵
for i in hello.values():print(i,end='')# 更新,將字典合并了,hello包含hello和hello2的所有元素
hello.update(hello2)
print(hello)# 循環鍵和值
for i in hello:print(i,hello[i])for k,v in hello.items():print(k,v)# 綜合以上比較,
# 字典對象.keys() #以列表形式返回所有的的鍵,沒有值
# 字典對象.values() #以列表返回所有字典的值,沒有鍵(另外在Django orm 中,models.table.objects.values()是元組,values_list是字典)
# 字典對象.items() 以列表返回鍵和值組成的元組,因此for循環k,v可以取出鍵和值