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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Python 哈希教程 > 原文: [http://zetcode.com/python/hashing/](http://zetcode.com/python/hashing/) Python 哈希教程解釋了 Python 中的哈希概念。 我們介紹了哈希表和 Python 可哈希對象。 ## 哈希表 哈希表用于以許多常見的編程語言(例如 C++ ,Java 和 Python)實現映射和設置數據結構。 Python 將哈希表用于字典和集合。 哈希表是鍵值對的無序集合,其中每個鍵都是唯一的。 哈希表提供了有效的查找,插入和刪除操作的組合。 這些是數組和鏈表的最佳屬性。 ## 哈希 哈希是使用算法將任意大小的數據映射到固定長度的過程。 這稱為哈希值。 哈希用于創建高性能,直接訪問的數據結構,在該結構中要快速存儲和訪問大量數據。 哈希值使用哈希函數計算。 ## Python 可哈希對象 如果對象的哈希值在其生命周期內從未發生變化,則該對象是可哈希的。 (在多次調用 Python 程序期間,它可以具有不同的值。)可哈希對象需要`__hash__()`方法。 為了執行比較,哈希需要一種`__eq__()`方法。 > **注意**:比較相等的可哈希對象必須具有相同的哈希值。 哈希性使對象可用作字典鍵和集成員,因為這些數據結構在內部使用哈希值。 Python 不可變的內置對象是可哈希的; 可變容器(例如列表或字典)不是。 默認情況下,作為用戶定義類實例的對象是可哈希的。 它們都比較不相等(除了它們本身),并且它們的哈希值是從`id()`派生的。 > **注意**:如果一個類沒有定義一個`__eq __()`方法,它也不應該定義一個`__hash __()`操作。 如果它定義了`__eq __()`而不是`__hash __()`,則其實例將不能用作可哈希集合中的項目。 ## Python `hash()`函數 `hash()`函數返回對象的哈希值(如果有的話)。 哈希值是整數。 它們用于在字典查找期間快速比較字典關鍵字。 對象可以實現`__hash__()`方法。 ## Python 不可變內置函數可哈希化 Python 不變的內置函數(例如整數,字符串或元組)是可哈希的。 `builtin_hashables.py` ```py #!/usr/bin/env python3 val = 100 print(val.__hash__()) print("falcon".__hash__()) print((1,).__hash__()) ``` 該示例顯示三個哈希值的值:整數,字符串和元組。 ## Python 自定義可哈希對象示例 I Python 自定義對象默認情況下是可哈希的。 他們的哈希值是從其 ID 派生的。 `custom_object.py` ```py #!/usr/bin/env python3 class User: def __init__(self, name, occupation): self.name = name self.occupation = occupation u1 = User('John Doe', 'gardener') u2 = User('John Doe', 'gardener') print('hash of user 1') print(hash(u1)) print('hash of user 2') print(hash(u2)) if (u1 == u2): print('same user') else: print('different users') ``` 在示例中,我們有`User`的兩個實例。 ```py u1 = User('John Doe', 'gardener') u2 = User('John Doe', 'gardener') ``` 我們有兩個具有相同數據的實例。 ```py print('hash of user 1') print(hash(u1)) ``` `hash()`函數返回對象的哈希值。 默認實現是從對象的 ID 派生的。 ```py $ python custom_object.py hash of user 1 -9223371894419573195 hash of user 2 142435202673 different users ``` 即使用戶詳細信息相同,但比較仍會產生不同的對象。 為了更改它,我們需要實現`__eq__()`方法。 ## Python 自定義可哈希對象示例 II 在第二個示例中,我們實現了自定義`__eq__()`方法。 `custom_object2.py` ```py #!/usr/bin/env python3 class User: def __init__(self, name, occupation): self.name = name self.occupation = occupation def __eq__(self, other): return self.name == other.name \ and self.occupation == other.occupation def __str__(self): return f'{self.name} {self.occupation}' u1 = User('John Doe', 'gardener') u2 = User('John Doe', 'gardener') if (u1 == u2): print('same user') print(f'{u1} == {u2}') else: print('different users') # users = {u1, u2} # print(len(users)) ``` 現在比較返回給我們的預期輸出; 但是,我們不能將對象插入 Python 集中; 這將導致`TypeError: unhashable type: 'User'`。 為了更改此設置,我們實現了`__hash__()`方法。 ## Python 自定義可哈希對象示例 III 在第三個示例中,我們實現了`__eq__()`和`__hash__()`方法。 `custom_object3.py` ```py #!/usr/bin/env python3 class User: def __init__(self, name, occupation): self.name = name self.occupation = occupation def __eq__(self, other): return self.name == other.name \ and self.occupation == other.occupation def __hash__(self): return hash((self.name, self.occupation)) def __str__(self): return f'{self.name} {self.occupation}' u1 = User('John Doe', 'gardener') u2 = User('John Doe', 'gardener') users = {u1, u2} print(len(users)) if (u1 == u2): print('same user') print(f'{u1} == {u2}') else: print('different users') print('------------------------------------') u1.occupation = 'programmer' users = {u1, u2} print(len(users)) if (u1 == u2): print('same user') print(f'{u1} == {u2}') else: print('different users') ``` 該示例比較了具有`__eq__()`和`__hash__()`方法的自定義實現的兩個對象。 可以將這些對象插入 Python 集中,當以后更改屬性時,我們將獲得預期的輸出。 ```py def __hash__(self): return hash((self.name, self.occupation)) ``` `__hash__()`函數的實現從屬性元組返回使用`hash()`函數計算的哈希值。 ```py $ python custom_object3.py 1 same user John Doe gardener == John Doe gardener ------------------------------------ 2 different users ``` 這是輸出。 ## Python `@dataclass`裝飾器 從 Python 3.7 開始,我們有了`dataclass`裝飾器,它會自動生成一些樣板代碼。 數據類裝飾器的凍結參數(默認為`False`)。 如果指定,則字段將被凍結(即只讀)。 如果`eq`設置為`True`(默認情況下),則將實現`__hash__()`方法,并且對象實例將是可哈希的。 `decorator.py` ```py #!/usr/bin/env python3 from dataclasses import dataclass @dataclass(frozen=True) class User: name: str occupation: str u1 = User('John Doe', 'gardener') u2 = User('John Doe', 'gardener') if (u1 == u2): print('same user') print(f'{u1} == {u2}') else: print('different users') users = {u1, u2} print(len(users)) ``` 該示例使用`@dataclass`裝飾器。 ```py $ python decorator.py same user User(name='John Doe', occupation='gardener') == User(name='John Doe', occupation='gardener') 1 ``` 這是輸出。 在本教程中,我們介紹了 Python 中的哈希。 您可能也對以下相關教程感興趣: [Python 教程](/lang/python/), [Python 列表推導](/articles/pythonlistcomprehensions/)或列表[所有 Python 教程](/all/#python)。
                  <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>

                              哎呀哎呀视频在线观看