python day5 字典

來源:互聯網
上載者:User

標籤:size   inpu   define   pytho   not   update   fine   列表   元組   

python字典
# 資料類型劃分: 可變資料類型、不可變資料類型# 不可變資料類型: 元祖 bool str int  可雜湊# 可變資料類型 : list dict(字典) set(集合) 不可雜湊# dict key(必須是可雜湊) value(任意資料類型)# dict 優點:查詢速度快(二分尋找)#            儲存大量的關係型資料#      特點:無序的(3.5版本以前)dic1 = {"age": 18, "name": "Break", "sex": "male"}# 增dic1["hobby"] = "girly"  # 沒有就增加# print(dic1)     # {‘age‘: 18, ‘name‘: ‘Break‘, ‘hobby‘: ‘giry‘, ‘sex‘: ‘male‘}dic1.setdefault("game")  # 沒有就增加,有就覆蓋(增加的預設值是None)# print(dic1)     # {‘sex‘: ‘male‘, ‘age‘: 18, ‘name‘: ‘Break‘, ‘game‘: None, ‘hobby‘: ‘girly‘}# 刪dic1.pop("game")  # 傳入要刪除的key (傳回值是對應key的value)# print(dic1.pop("Break666", "沒有對應的Key(none)"))   # 如果需要刪除的key沒有的話 會返回第二個參數 <沒有對應的Key(none)># print(dic1)     # {‘hobby‘: ‘girly‘, ‘age‘: 18, ‘sex‘: ‘male‘, ‘name‘: ‘Break‘}# print(dic1.popitem())  # 隨機刪除 3.6版本以後預設刪除最後一個 返回一個元組 (‘name‘, ‘Break‘)# print(dic1)     # {‘hobby‘: ‘girly‘, ‘sex‘: ‘male‘, ‘age‘: 18}del dic1["hobby"]  # 刪除對應的key 如果key沒有就會報錯,建議用pop# print(dic1)   # {‘sex‘: ‘male‘, ‘name‘: ‘Break‘, ‘age‘: 18}# del dic1    # 直接刪除字典# print(dic1)     # 此時會報錯 NameError: name ‘dic1‘ is not defined# dic1.clear()    # 清空字典# print(dic1)     # {}#  改dic1["age"] = 19  # 直接賦值print(dic1)# updatediv = {    "name": "box",    "width": "200px",    "height": "200px",    "background-color": "red"}dic1.update(div)  # 將div更新到dic1裡面 只改變dic1,div不改變# print(dic1)     # {‘sex‘: ‘male‘, ‘height‘: ‘200px‘, ‘width‘: ‘200px‘, ‘age‘: 19, ‘name‘: ‘box‘, ‘background-color‘: ‘red‘}# print(div)  # {‘name‘: ‘box‘, ‘height‘: ‘200px‘, ‘width‘: ‘200px‘, ‘background-color‘: ‘red‘}# 查 當做列表用# print(dic1.keys(), type(#     dic1.keys()))# type是字典key類型(dict_keys) dict_keys([‘background-color‘, ‘age‘, ‘width‘, ‘name‘, ‘sex‘, ‘height‘])#  <class ‘dict_keys‘># print(dic1.values(), type(#     dic1.values()))# type是字典values類型(dict_values) dict_values([‘red‘, 19, ‘200px‘, ‘box‘, ‘male‘, ‘200px‘]) <class# ‘dict_values‘># print(dic1.items(), type(#     dic1.items()))# type是字典items類型(dict_items) dict_items([(‘background-color‘, ‘red‘), (‘age‘, 19), (‘width‘, ‘200px‘), (‘name‘,# ‘box‘), (‘sex‘, ‘male‘), (‘height‘, ‘200px‘)]) <class ‘dict_items‘>a, b, c = [1, 2, 3]print(a, b, c)‘‘‘for i in dic1:    print(i)    # 全部是key    print(dic1[i])      # 全是value‘‘‘# for k, v in dic1.items():#     print(k, v)# get擷取索引值# print(dic1["name1"])    # 報錯# print(dic1.get("name1", "沒有這個key"))   # 沒有預設返回none 可以改變預設傳回值‘‘‘    info = input(">>>")    for i in info:        if i.isalpha():            info = info.replace(i, " ")    print(len(info.split()))‘‘‘

  

python day5 字典

相關文章

聯繫我們

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