1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      Python中set和dict的案例分析-創(chuàng)新互聯(lián)

      創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

      目前創(chuàng)新互聯(lián)公司已為上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機網(wǎng)站運營、企業(yè)網(wǎng)站設(shè)計、大悟網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

      小編給大家分享一下Python中set和dict的案例分析,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

      dict常用操作

      引言

      clear(): 清空字典

      copy(): 返回一個淺拷貝

      fromkeys(): 將可迭代對象中的每一個元素作為key和同一個value拼成字典

      get(): 根據(jù)key返回value,若無對應(yīng)的鍵值對,則返回None,也可以指定默認(rèn)返回值,和索引訪問相比,不會產(chǎn)生異常。

      items():返回一個dict_items類型,支持迭代,鍵值對以元組形式組織

      setdefault(): 獲取key對應(yīng)的value值,先調(diào)用get(),若不存在該鍵值對,則添加

      update(): 合并字典,或鍵值對元組構(gòu)成的可迭代對象

      使用案例

      # 1. clear()
      d = {name:"MetaTian", age:"22"}
      d.clear()
      
      # 2. copy()
      new_dict = d.copy()
      new_dict["age"] = 18
      
      print(new_dict)
      print(d)
      # resutl:
      # {'age': 18, 'name': 'MetaTian'}
      # {'age': '22', 'name': 'MetaTian'}
      
      # 3. fromkeys()
      d = dict.fromkeys(range(3), "MetaTian")
      print(d)
      # result:
      # {0: 'MetaTian', 1: 'MetaTian', 2: 'MetaTian'}
      
      # 4. get()
      print(d.get(2))
      print(d.get(3))
      print(d.get(3, "null"))
      
      # result:
      # MetaTian
      # None
      # null
      
      # 5. items()
      print(type(d.items()))
      print(d.items())
      # result:
      # 
      # dict_items([(0, 'MetaTian'), (1, 'MetaTian'), (2, 'MetaTian')])
      
      # 6. setdefault()
      d = {}
      value = d.setdefault("name", "MetaTian")  # 如果無 name 這個 key,則添加
      print(value, d)
      # result:
      # MetaTian {'name': 'MetaTian'}
      
      # 7. update()
      d1 = {1:"a"}
      d2 = {2:"b"}
      
      d1.update(d2)
      d2.update([(3, "c"), (4, "d")])
      
      print(d1)
      print(d2)
      # result:
      # {1: 'a', 2: 'b'}
      # {2: 'b', 3: 'c', 4: 'd'}

      set和frozenset

      引言

      set是可變集合,frozenset是不可變集合

      集合中的元素?zé)o序,不重復(fù)

      使用案例

      """
      通過 set(Iterable) 來構(gòu)建出可變集合對象
      通過 frozenset(Iterable) 構(gòu)建不可變集合對象
      """
      s = set("12345666")
      fs = frozenset(['a', 'b', 'c', 'a'])    # 不可變類型,可以作為 dict 的 key
      print(s)
      print(fs)
      
      # result:
      # {'6', '1', '4', '5', '3', '2'}
      # frozenset({'b', 'a', 'c'})
      
      """
      向 set 中添加元素
      add()
      update()
      """
      s1, s2 = set("123"), set("234")
      s1.update(s2)
      s2.add('5')
      
      print(s1)
      print(s2)
      
      # result:
      # {'1', '2', '3', '4'}
      # {'2', '3', '5', '4'}
      
      """
      集合的運算
      - 差
      & 交
      | 并
      """
      s1, s2 = set("123"), set("234")
      print(s1 - s2)
      print(s1 & s2)
      print(s1 | s2)
      
      # result:
      # {'1'}
      # {'2', '3'}
      # {'3', '1', '2', '4'}

      dict和set的實現(xiàn)原理

      引言

      dict和set的查找性能遠(yuǎn)遠(yuǎn)大于list

      dict和set底層通過散列表存儲,因此也要求dict的key是可哈希的,不可變對象都是可哈希的

      哈希的原理.

      以字典為例.

      存儲之前要通過哈希函數(shù)來計算key的值,得到存儲索引,如果得到的結(jié)果已經(jīng)被使用,要處理沖突,重新計算后再進行存儲

      自定義的類通過實現(xiàn)__hash__(),就可以存儲在dict和set中.

      看完了這篇文章,相信你對Python中set和dict的案例分析有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝各位的閱讀!


      當(dāng)前題目:Python中set和dict的案例分析-創(chuàng)新互聯(lián)
      文章鏈接:http://www.ef60e0e.cn/article/csdgcp.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        朝阳县| 怀远县| 长沙市| 长治县| 海原县| 顺昌县| 汤原县| 泽普县| 岳阳市| 志丹县| 开阳县| 聊城市| 育儿| 宁津县| 桃江县| 乌什县| 黑山县| 安宁市| 佛山市| 太仆寺旗| 田阳县| 漯河市| 青龙| 潜山县| 榆林市| 山西省| 合江县| 潜山县| 余江县| 晋宁县| 临西县| 苏尼特左旗| 上思县| 吉木萨尔县| 八宿县| 资阳市| 万源市| 正镶白旗| 寿宁县| 蒙城县| 延庆县|