dict(字典)

來源:互聯網
上載者:User

標籤:def   lis   get   style   upd   淺拷貝   刪除   根據   list   

建立字典:info = {

“age”: 18,

“name”: “zhangsan”,

“li”: [ 11,[],(),22,33,{“k1” :  “vv1”, “sex” : 0} ] ,

}

(1)字典的keys可以是數字、字串、元組,keys不能重複,否則第二個以後的key無法顯示

(2)取值:info[“age”]、info.get(“age”,default):值不存在用default填充

    區別:info[“age”]的key不存在則報錯,get()不會

(3)clear(),清空字典

(4)copy(),複製字典(淺拷貝)

(5)del info[keys],通過key刪除字典元素

(6)for item in info: 迴圈得到key,item為info的key

(7)for item in info.keys(): 迴圈得到keys

(8)for item in info.values(): 迴圈得到value

(9)for k,v in info.item():迴圈得到key和value

(10)v = dict.fromkeys(list, value),根據序列list的每一個元素作為key,value作為值產生字典

(11)pop(key),刪除key為key的元素,並返回被刪除的元素

(12)popitem(),隨機刪除,並返回key、value索引值對

(13)setdefault(key, value),設定值。key已存在,不設定,返回當前key值,key不存在則設定key、value,並返回value

(14)update({key1:value1, key2:value2})

     或update(key1=value1, key2=value2),更新value值

dict(字典)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.