<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          技巧 | Python 字典用法詳解(超全)

          共 6074字,需瀏覽 13分鐘

           ·

          2021-03-16 12:19


          本期導讀

          字典(Dictionary)是Python提供的一種常用的數(shù)據(jù)結構,它用于存放具有映射關系的數(shù)據(jù)。本期給大家?guī)鞵ython字典11個方法的全面解析,希望對你有所幫助。

          字典由鍵(key)和值(value)成對組成,鍵和值中間以冒號:隔開,項之間用逗號隔開,整個字典由大括號 {} 括起來

          格式如下:

          dic = {key1 : value1, key2 : value2 }

          字典也被稱作關聯(lián)數(shù)組或哈希表
          下面是幾種常見的字典創(chuàng)建方式

          # 方法1
          dic1 = { 'Author' : 'Python當打之年' , 'age' : 99 , 'sex' : '男' }

          # 方法2
          lst = [('Author', 'Python當打之年'), ('age', 99), ('sex', '男')]
          dic2 = dict(lst)

          # 方法3
          dic3 = dict( Author = 'Python當打之年', age = 99, sex = '男')

          # 方法4
          list1 = ['Author', 'age', 'sex']
          list2 = ['Python當打之年', 99, '男']
          dic4 = dict(zip(list1, list2))

          字典創(chuàng)建的方式還有很多種,這里不再贅述。


          接下來是重點

          接下來是重點

          接下來是重點


          字典由 dict 類代表,可以使用 dir(dict) 來查看該類包含哪些方法,輸入命令,可以看到如下輸出結果:

          methods = dir(dict)
          print('methods = ',methods)

          methods = ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

          字典的方法和屬性有很多種,這里我們重點介紹以下 11 種方法

          ['clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']


          1、dict.clear()

          clear() 用于清空字典中所有元素(鍵-值對),對一個字典執(zhí)行 clear() 方法之后,該字典就會變成一個空字典。

          list1 = ['Author', 'age', 'sex']
          list2 = ['Python當打之年', 99, '男']
          dic1 = dict(zip(list1, list2))
          # dic1 = {'Author': 'Python當打之年', 'age': 99, 'sex': '男'}

          dic1.clear()
          # dic1 = {}



          2、dict.copy()
          copy() 用于返回一個字典的淺拷貝。
          list1 = ['Author', 'age', 'sex']
          list2 = ['Python當打之年', 99, '男']
          dic1 = dict(zip(list1, list2))

          dic2 = dic1 # 淺拷貝: 引用對象
          dic3 = dic1.copy() # 淺拷貝:深拷貝父對象(一級目錄),子對象(二級目錄)不拷貝,還是引用
          dic1['age'] = 18

          # dic1 = {'Author': 'Python當打之年', 'age': 18, 'sex': '男'}
          # dic2 = {'Author': 'Python當打之年', 'age': 18, 'sex': '男'}
          # dic3 = {'Author': 'Python當打之年', 'age': 99, 'sex': '男'}
          其中 dic2 是 dic1 的引用,所以輸出結果是一致的,dic3 父對象進行了深拷貝,不會隨dic1 修改而修改,子對象是淺拷貝所以隨 dic1 的修改而修改,注意父子關系。
          拓展深拷貝:copy.deepcopy()
          import copy

          list1 = ['Author', 'age', 'sex']
          list2 = ['Python當打之年', [18,99], '男']
          dic1 = dict(zip(list1, list2))

          dic2 = dic1
          dic3 = dic1.copy()
          dic4 = copy.deepcopy(dic1)
          dic1['age'].remove(18)
          dic1['age'] = 20

          # dic1 = {'Author': 'Python當打之年', 'age': 20, 'sex': '男'}
          # dic2 = {'Author': 'Python當打之年', 'age': 20, 'sex': '男'}
          # dic3 = {'Author': 'Python當打之年', 'age': [99], 'sex': '男'}
          # dic4 = {'Author': 'Python當打之年', 'age': [18, 99], 'sex': '男'}
          dic2 是 dic1 的引用,所以輸出結果是一致的;dic3 父對象進行了深拷貝,不會隨dic1 修改而修改,子對象是淺拷貝所以隨 dic1 的修改而修改;dic4 進行了深拷貝,遞歸拷貝所有數(shù)據(jù),相當于完全在另外內存中新建原字典,所以修改dic1不會影響dic4的數(shù)據(jù)

          3、dict.fromkeys()
          fromkeys() 使用給定的多個鍵創(chuàng)建一個新字典,值默認都是 None,也可以傳入一個參數(shù)作為默認的值。
          list1 = ['Author', 'age', 'sex']
          dic1 = dict.fromkeys(list1)
          dic2 = dict.fromkeys(list1, 'Python當打之年')

          # dic1 = {'Author': None, 'age': None, 'sex': None}
          # dic2 = {'Author': 'Python當打之年', 'age': 'Python當打之年', 'sex': 'Python當打之年'}


          4、dict.get()
          get() 用于返回指定鍵的值,也就是根據(jù)鍵來獲取值,在鍵不存在的情況下,返回 None,也可以指定返回值。
          list1 = ['Author', 'age', 'sex']
          list2 = ['Python當打之年', [18,99], '男']
          dic1 = dict(zip(list1, list2))

          Author = dic1.get('Author')
          # Author = Python當打之年
          phone = dic1.get('phone')
          # phone = None
          phone = dic1.get('phone','12345678')
          # phone = 12345678

          5、dict.items()
          items() 獲取字典中的所有鍵-值對,一般情況下可以將結果轉化為列表再進行后續(xù)處理。
          list1 = ['Author', 'age', 'sex']
          list2 = ['Python當打之年', [18,99], '男']
          dic1 = dict(zip(list1, list2))
          items = dic1.items()
          print('items = ', items)
          print(type(items))
          print('items = ', list(items))

          # items = dict_items([('Author', 'Python當打之年'), ('age', [18, 99]), ('sex', '男')])
          # <class 'dict_items'>
          # items = [('Author', 'Python當打之年'), ('age', [18, 99]), ('sex', '男')]


          6、dict.keys()
          keys() 返回一個字典所有的鍵。
          list1 = ['Author', 'age', 'sex']
          list2 = ['Python當打之年', [18,99], '男']
          dic1 = dict(zip(list1, list2))
          keys = dic1.keys()
          print('keys = ', keys)
          print(type(keys))
          print('keys = ', list(keys))

          # keys = dict_keys(['Author', 'age', 'sex'])
          # <class 'dict_keys'>
          # keys = ['Author', 'age', 'sex']

          7、dict.pop()
          pop() 返回指定鍵對應的值,并在原字典中刪除這個鍵-值對。
          list1 = ['Author', 'age', 'sex']
          list2 = ['Python當打之年', [18,99], '男']
          dic1 = dict(zip(list1, list2))
          sex = dic1.pop('sex')
          print('sex = ', sex)
          print('dic1 = ',dic1)

          # sex = 男
          # dic1 = {'Author': 'Python當打之年', 'age': [18, 99]}


          8、dict.popitem()
          popitem() 刪除字典中的最后一對鍵和值。
          list1 = ['Author', 'age', 'sex']
          list2 = ['Python當打之年', [18,99], '男']
          dic1 = dict(zip(list1, list2))
          dic1.popitem()
          print('dic1 = ',dic1)

          # dic1 = {'Author': 'Python當打之年', 'age': [18, 99]}


          9、dict.setdefault()
          setdefault() 和 get() 類似, 但如果鍵不存在于字典中,將會添加鍵并將值設為default。
          list1 = ['Author', 'age', 'sex']
          list2 = ['Python當打之年', [18,99], '男']
          dic1 = dict(zip(list1, list2))
          dic1.setdefault('Author', '當打之年')
          print('dic1 = ',dic1)
          # dic1 = {'Author': 'Python當打之年', 'age': [18, 99], 'sex': '男'}
          dic1.setdefault('name', '當打之年')
          print('dic1 = ',dic1)
          # dic1 = {'Author': 'Python當打之年', 'age': [18, 99], 'sex': '男', 'name': '當打之年'}


          10、dict.update(dict1)
          update() 字典更新,將字典dict1的鍵-值對更新到dict里,如果被更新的字典中己包含對應的鍵-值對,那么原鍵-值對會被覆蓋,如果被更新的字典中不包含對應的鍵-值對,則添加該鍵-值對。
          list1 = ['Author', 'age', 'sex']
          list2 = ['Python當打之年', [18,99], '男']
          dic1 = dict(zip(list1, list2))
          print('dic1 = ',dic1)
          # dic1 = {'Author': 'Python當打之年', 'age': [18, 99], 'sex': '男'}

          list3 = ['Author', 'phone' ]
          list4 = ['當打之年', 12345678]
          dic2 = dict(zip(list3, list4))
          print('dic2 = ',dic2)
          # dic2 = {'Author': '當打之年', 'phone': 12345678}

          dic1.update(dic2)
          print('dic1 = ',dic1)
          # dic1 = {'Author': '當打之年', 'age': [18, 99], 'sex': '男', 'phone': 12345678}

          11、dict.values()
          values() 返回一個字典所有的值。
          list1 = ['Author', 'age', 'sex']
          list2 = ['Python當打之年', [18,99], '男']
          dic1 = dict(zip(list1, list2))
          values = dic1.values()
          print('values = ', values)
          print(type(values))
          print('values = ', list(values))

          # values = dict_values(['Python當打之年', [18, 99], '男'])
          # <class 'dict_values'>
          # values = ['Python當打之年', [18, 99], '男']

          END
          瀏覽 42
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  黑人操| 亚洲日本色情视频在线 | 久久亚洲AV成人无码电影麻豆 | 亚洲高清视频无码 | 免费看黄片的视频 |