Python——字典,python字典

來源:互聯網
上載者:User

Python——字典,python字典

字典是一種key-value 的 資料類型,使用就想我們上學用的字典。可以通過筆畫,字母來查對應頁的詳細內容。

特性:
1. 字典是無須的。(如果光列印字典裡的字串,那麼排序不會按照順序排,因為字典是沒有下標的。)

基礎文法:
info = {

'stu1101': "TengLan Wu",

'stu1102': "LongZe Luola",

'stu1103': "XiaoZe Maliya",

}

尋找

print(info['stu1101']) #精確尋找,如果沒有就報錯

info.get('stu1108') #相當於模糊尋找,返回nunl

print('stu1103'in info) #查看該key是否有使用者

修改

info['stu1101']= '武藤蘭'

增加

info['stu1104']='cangjingkong'

刪除

info.pop('stu1101')

尋找建立

info.setdefault('stu1101',{'www.baidu.com':[1,2]}) #先查看是否有stu1101值,如果有那麼就列印原有的,如果沒有將建立後面的。

轉換

info.items() #改成列表形式

迴圈

for i in info:

print(i,info[i])

Python的字典的items(), keys(), values()都返回一個list
>>> dict = { 1 : 2, 'a' : 'b', 'hello' : 'world' }
>>> dict.values()     #返回所有value值
['b', 2, 'world']
>>> dict.keys()       #返回所有key值
['a', 1, 'hello']
>>> dict.items()      #返回所有值
[('a', 'b'), (1, 2), ('hello', 'world')]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.