<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 3.1.?Dictionary 介紹 * 3.1.1\. Dictionary 的定義 * 3.1.2\. Dictionary 的修改 * 3.1.3\. 從 dictionary 中刪除元素 Dictionary 是 Python 的內置數據類型之一,它定義了鍵和值之間一對一的關系。 > 注意 > Python 中的 dictionary 就像 Perl 中的 hash (哈希數組)。在 Perl 中,存儲哈希值的變量總是以 `%` 字符開始;在 Python 中,變量可以任意取名,并且 Python 在內部會記錄下其數據類型。 > 注意 > Python 中的 dictionary 像 Java 中的 `Hashtable` 類的實例。 > 注意 > Python 中的 dictionary 像 Visual Basic 中的 `Scripting.Dictionary` 對象的實例。 ## 3.1.1.?Dictionary 的定義 ## 例?3.1.?定義 Dictionary ``` >>> d = {"server":"mpilgrim", "database":"master"} >>> d {'server': 'mpilgrim', 'database': 'master'} >>> d["server"] 'mpilgrim' >>> d["database"] 'master' >>> d["mpilgrim"] Traceback (innermost last): File "<interactive input>", line 1, in ? KeyError: mpilgrim ``` | | | | --- | --- | | \[1\] | 首先我們創建了新 dictionary,它有兩個元素,將其賦給變量 `d` 。每一個元素都是一個 key-value 對;整個元素集合用大括號括起來。 | | \[2\] | `'server'` 是一個 key,它所關聯的值是通過 `d["server"]` 來引用的,為 `'mpilgrim'`。 | | \[3\] | `'database'` 是一個 key,它所關聯的值是通過 `d["database"]` 來引用的,為 `'master'`。 | | \[4\] | 您可以通過 key 來引用其值,但是不能通過值獲取 key。所以 `d["server"]` 的值為 `'mpilgrim'`,而使用 `d["mpilgrim"]` 會引發一個異常,因為 `'mpilgrim'` 不是一個 key。 | ## 3.1.2.?Dictionary 的修改 ## 例?3.2.?修改 Dictionary ``` >>> d {'server': 'mpilgrim', 'database': 'master'} >>> d["database"] = "pubs" >>> d {'server': 'mpilgrim', 'database': 'pubs'} >>> d["uid"] = "sa" >>> d {'server': 'mpilgrim', 'uid': 'sa', 'database': 'pubs'} ``` | | | | --- | --- | | \[1\] | 在一個 dictionary 中不能有重復的 key。給一個存在的 key 賦值會覆蓋原有的值。 | | \[2\] | 在任何時候都可以加入新的 key-value 對。這種語法同修改存在的值是一樣的。(是的,它可能某天會給您帶來麻煩。假設你一次次地修改一個 dictionary,但其間您使用的 key 并未按照您的想法進行改變。您可能以為加入了新值,但實際上只是一次又一次地修改了同一個值。) | 請注意新的元素 (key 為 `'uid'`,value 為 `'sa'`) 出現在中間。實際上,在第一個例子中的元素看上去是的有序不過是一種巧合。現在它們看上去的無序同樣是一種巧合。 > 注意 > Dictionary 沒有元素順序的概念。說元素 “順序亂了” 是不正確的,它們只是序偶的簡單排列。這是一個重要的特性,它會在您想要以一種特定的,可重現的順序 (像以 key 的字母表順序) 存取 dictionary 元素的時候騷擾您。有一些實現這些要求的方法,它們只是沒有加到 dictionary 中去。 當使用 dictionary 時,您需要知道:dictionary 的 key 是大小寫敏感的。 ## 例?3.3.?Dictionary 的 key 是大小寫敏感的 ``` >>> d = {} >>> d["key"] = "value" >>> d["key"] = "other value" >>> d {'key': 'other value'} >>> d["Key"] = "third value" >>> d {'Key': 'third value', 'key': 'other value'} ``` | | | | --- | --- | | \[1\] | 為一個已經存在的 dictionary key 賦值,將簡單覆蓋原有的值。 | | \[2\] | 這不會為一個已經存在的 dictionary key 賦值,因為在 Python 中是區分大小寫的,也就是說 `'key'` 與 `'Key'` 是不同的。所以這種情況將在 dictionary 中創建一個新的 key-value 對。雖然看上去很相近,但是在 Python 眼里是完全不同的。 | ## 例?3.4.?在 dictionary 中混用數據類型 ``` >>> d {'server': 'mpilgrim', 'uid': 'sa', 'database': 'pubs'} >>> d["retrycount"] = 3 >>> d {'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 'retrycount': 3} >>> d[42] = "douglas" >>> d {'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 42: 'douglas', 'retrycount': 3} ``` | | | | --- | --- | | \[1\] | Dictionary 不只是用于存儲字符串。Dictionary 的值可以是任意數據類型,包括字符串、整數、對象,甚至其它的 dictionary。在單個 dictionary 里,dictionary 的值并不需要全都是同一數據類型,可以根據需要混用和匹配。 | | \[2\] | Dictionary 的 key 要嚴格多了,但是它們可以是字符串、整數或幾種其它的類型 (后面還會談到這一點)。也可以在一個 dictionary 中混用和匹配 key 的數據類型。 | ## 3.1.3.?從 dictionary 中刪除元素 ## 例?3.5.?從 dictionary 中刪除元素 ``` >>> d {'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 42: 'douglas', 'retrycount': 3} >>> del d[42] >>> d {'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 'retrycount': 3} >>> d.clear() >>> d {} ``` | | | | --- | --- | | \[1\] | `del` 允許您使用 key 從一個 dictionary 中刪除獨立的元素。 | | \[2\] | `clear` 從一個 dictionary 中清除所有元素。注意空的大括號集合表示一個沒有元素的 dictionary。 | ## 進一步閱讀 * _How to Think Like a Computer Scientist_ 講授了 dictionary 和如何[使用 dictionary 模擬稀疏矩陣](http://www.ibiblio.org/obp/thinkCSpy/chap10.htm)。 * Python Knowledge Base 有許多[使用 dictionary 的示例代碼](http://www.faqts.com/knowledge-base/index.phtml/fid/541)。 * Python Cookbook 討論了[如何通過 key 對 dictionary 的值進行排序](http://www.activestate.com/ASPN/Python/Cookbook/Recipe/52306)。 * _Python Library Reference_ 總結了[所有的 dictionary 方法](http://www.python.org/doc/current/lib/typesmapping.html)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看