<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 字典 的用法

          共 2519字,需瀏覽 6分鐘

           ·

          2019-11-30 23:20

          d2cea3792e1e532f40a6dbcd675f1782.webp

          作者:劉帥

          博文地址:https://www.cnblogs.com/liu-shuai/


          字典是Python語言中唯一的映射類型。字典對(duì)象是可變的,它是一個(gè)容器類型,支持異構(gòu)、任意嵌套。


          創(chuàng)建字典


            語法:{key1:val1,key2:val2,.....}

            dict1 = {}      #創(chuàng)建空字典

            dict2 = {'n1':'liush','n2':'spirit','n3':'tester'}

            使用函數(shù)dict創(chuàng)建字典


          >>>D = dict(name='spititman',age=28,gender='M')2 >>>print D3 {'gender': 'M', 'age': 28, 'name': 'spititman'}

          使用zip和dict創(chuàng)建字典

          zip語法: 2     zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)] 3 實(shí)例展示: 4     >>>L = zip('xyz','123') 5     >>>print L 6     [('x', '1'), ('y', '2'), ('z', '3')] 7  8     >>>L = zip('xyz','123','abc') 9     >>>print L10     [('x', '1', 'a'), ('y', '2', 'b'), ('z', '3', 'c')]11 ################################################################################12 >>>D = dict(zip('xyz','123'))13 >>>print D14 {'y': '2', 'x': '1', 'z': '3'}15 16 >>>D = dict(zip('xyz','123','abc'))17 >>>print D18 ValueError: dictionary update sequence element #0 has length 3; 2 is required


          字典常用操作及實(shí)例展示


            可以使用dir(dict)查看字典支持的操作方法


          clear

            功能:清空字典所有元素

            語法:D.clear() -> None. ?Remove all items from D

            實(shí)例展示:

          >>>D = {'n1':'liush','n2':'spirit','n3':'tester'}2 >>>D.clear()3 >>>print D4 {}

          copy

            功能:淺復(fù)制字典。

            語法:D.copy() -> a shallow copy of D

            實(shí)例展示:

          >>>D = {'n1':'liush','n2':'spirit','n3':'tester'}2 >>>id(D)3 1403882119112084 >>>D1 = D.copy()5 >>>print D16 {'n1': 'liush', 'n2': 'spirit', 'n3': 'tester'}7 >>>id(D1)8 140388110074776


          fromkeys

            功能:用于創(chuàng)建一個(gè)新字典,以序列S中的元素作為字典的鍵,v為新字典中所有鍵對(duì)應(yīng)的初始值,默認(rèn)為none。

            語法:dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.v defaults to None

            實(shí)例展示:

          >>>L = ['spirit','man','liush']2 >>>D_L = dict.fromkeys(L)3 >>>print D_L4 {'liush': None, 'spirit': None, 'man': None}5 ########################################################6 >>>D_L = dict.fromkeys(L,'test')7 >>>print D_L8 {'liush': 'test', 'spirit': 'test', 'man': 'test'}

          get

            功能:獲取指定鍵的值

            語法:D.get(k[,d]) -> D[k] if k in D, else d. ?d defaults to None

            實(shí)例展示:

          >>>D = {'n1': 'liushuai', 'n2': 'spirit', 'n3': 'tester'} 2 >>>D1 = D.get('n4') 3 >>>print D1 4 none                    #n4不在字典D中,返回默認(rèn)值none 5 ########################################################## 6 >>>D2 = D.get('n4','check') 7 >>>print D2 8 check                   #n4不在字典D中,返回指定值check 9 ##########################################################10 >>>D3 = D.get('n2')11 >>>print D312 spirit                  #n2在字典D中,返回n2對(duì)應(yīng)的值13 ##########################################################14 >>>D4 = D.get('n2','check')15 >>>print D416 spirit                  #n2在字典D中,指定值無效,依然返回其對(duì)應(yīng)值


          未完,待續(xù)!


          覺得不錯(cuò),點(diǎn)個(gè)在看唄!

          瀏覽 108
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  色屁屁影院 | 国家产在线观看一二 | 操逼操欧美 | 亚洲黄色在线 | 五月婷婷六月色 |