<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Python 字典 > 原文: [https://thepythonguru.com/python-dictionaries/](https://thepythonguru.com/python-dictionaries/) * * * 于 2020 年 1 月 7 日更新 * * * 字典是一種 python 數據類型,用于存儲鍵值對。 它使您可以使用鍵快速檢索,添加,刪除,修改值。 字典與我們在其他語言上稱為關聯數組或哈希的非常相似。 **注意**: 字典是可變的。 ## 創建字典 * * * 可以使用一對大括號(`{}`)創建字典。 字典中的每個項目都由一個鍵,一個冒號,一個值組成。 每個項目都用逗號(`,`)分隔。 讓我們舉個例子。 ```py friends = { 'tom' : '111-222-333', 'jerry' : '666-33-111' } ``` 這里`friends`是有兩個項目的字典。 需要注意的一點是,鍵必須是可哈希的類型,但是值可以是任何類型。 字典中的每個鍵都必須是唯一的。 ```py >>> dict_emp = {} # this will create an empty dictionary ``` ## 檢索,修改和向字典中添加元素 * * * 要從字典中獲取項目,請使用以下語法: ```py >>> dictionary_name['key'] ``` ```py >>> friends['tom'] '111-222-333' ``` 如果字典中存在鍵,則將返回值,否則將引發`KeyError`異常。 要添加或修改項目,請使用以下語法: ```py >>> dictionary_name['newkey'] = 'newvalue' ``` ```py >>> friends['bob'] = '888-999-666' >>> friends ?{'tom': '111-222-333', 'bob': '888-999-666', 'jerry': '666-33-111'} ``` ## 從字典中刪除項目 * * * ```py >>> del dictionary_name['key'] ``` ```py >>>??del friends['bob'] >>> ?friends {'tom': '111-222-333', 'jerry': '666-33-111'} ``` 如果找到鍵,則該項目將被刪除,否則將拋出`KeyError`異常。 ## 遍歷字典中的項目 * * * 您可以使用`for`循環遍歷字典中的元素。 ```py >>> friends = { ... 'tom' : '111-222-333', ... 'jerry' : '666-33-111' ...} >>> >>> for key in friends: ... print(key, ":", friends[key]) ... tom : 111-222-333 jerry : 666-33-111 >>> >>> ``` ## 查找字典的長度 * * * 您可以使用`len()`函數查找字典的長度。 ```py >>> len(friends) 2 ``` ## `in`和`not in`運算符 * * * `in`和`not in`運算符檢查字典中是否存在鍵。 ```py >>> 'tom' in friends True >>> 'tom' not in friends False ``` ## 字典中的相等測試 * * * `==`和`!=`運算符告訴字典是否包含相同的項目。 ```py >>> d1 = {"mike":41, "bob":3} >>> d2 = {"bob":3, "mike":41} >>> d1 == d2 True >>> d1 != d2 False >>> ``` **注意**: 您不能使用`<`,`>`,`>=`,`<=`等其他關系運算符來比較字典。 ## 字典方法 * * * Python 提供了幾種內置的方法來處理字典。 | 方法 | 描述 | | --- | --- | | `popitem()` | 返回字典中隨機選擇的項目,并刪除所選項目。 | | `clear()` | 刪除字典中的所有內容 | | `keys()` | 以元組形式返回字典中的鍵 | | `values()` | 以元組形式返回字典中的值 | | `get(key)` | 鍵的返回值,如果找不到鍵,則返回`None`,而不是引發`KeyError`異常 | | `pop(key)` | 從字典中刪除該項目,如果找不到該鍵,則會拋出`KeyError` | ```py >>> friends = {'tom': '111-222-333', 'bob': '888-999-666', 'jerry': '666-33-111'} >>> >>> friends.popitem() ('tom', '111-222-333') >>> >>>?friends.clear() >>> >>>?friends {} >>> >>>?friends = {'tom': '111-222-333', 'bob': '888-999-666', 'jerry': '666-33-111'} >>> >>> friends.keys() dict_keys(['tom', 'bob', 'jerry']) >>> >>> friends.values() dict_values(['111-222-333', '888-999-666', '666-33-111']) >>> >>>?friends.get('tom') '111-222-333' >>> >>>?friends.get('mike', 'Not Exists') 'Not Exists' >>> >>>?friends.pop('bob') '888-999-666' >>> >>> friends {'tom': '111-222-333', 'jerry': '666-33-111'} ``` 在下一篇文章中,我們將學習 [Python 元組](/python-tuples/)。 * * * * * *
                  <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>

                              哎呀哎呀视频在线观看