Hướng dẫn python dictionaries immutable - từ điển python bất biến
Show
Python’s built-in collection types come in mutable and immutable flavours, but one is conspicuously missing: Nội dung chính ShowShow
Đâu là ____ ____1111? Nó có thể hữu ích… PEP 416 đã đề xuất một loại In [1]: power_levels["Kevin"] Out[1]: 9001 In [2]: power_levels["Benny"] Out[2]: 8000 In [3]: list(power_levels.keys()) Out[3]: ['Kevin', 'Benny']1 cho Python 3.3, vào năm 2012. PEP đã bị từ chối, vì một số lý do chính đáng. Lý do bao gồm một số câu hỏi về tiện ích của một dict bất biến, đáng để kiểm tra trước khi bạn thêm chúng vào mã của mình. Nhưng PEP đã cung cấp cho chúng tôi một công cụ để mô phỏng các ____10s bất biến: In [1]: power_levels["Kevin"] Out[1]: 9001 In [2]: power_levels["Benny"] Out[2]: 8000 In [3]: list(power_levels.keys()) Out[3]: ['Kevin', 'Benny']4. Loại này là một proxy chỉ đọc cho ____10 hoặc ánh xạ khác. Python sử dụng loại này trong nội bộ cho các từ điển quan trọng, đó là lý do tại sao bạn có thể các loại tích hợp khỉ tích. Thay đổi duy nhất trong Python 3.3 là để hiển thị loại này cho mã người dùng. . Cách sử dụng In [1]: power_levels["Kevin"] Out[1]: 9001 In [2]: power_levels["Benny"] Out[2]: 8000 In [3]: list(power_levels.keys()) Out[3]: ['Kevin', 'Benny'] 8Cách tạo các bản sao đột biến Gói bữa tiệc thridLà một dict bất biến? In [1]: power_levels["Kevin"] Out[1]: 9001 In [2]: power_levels["Benny"] Out[2]: 8000 In [3]: list(power_levels.keys()) Out[3]: ['Kevin', 'Benny'] Là một con trăn bất biến từ điển? In [4]: power_levels["Benny"] = 9200 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-39fccb3e4f76> in <module> ----> 1 power_levels["Benny"] = 9200 TypeError: 'mappingproxy' object does not support item assignment0: In [4]: power_levels["Benny"] = 9200 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-4-39fccb3e4f76> in <module> ----> 1 power_levels["Benny"] = 9200 TypeError: 'mappingproxy' object does not support item assignment Noice. Chúng ta có thể làm cho từ điển bất biến trong Python không? Tại sao Dict Keys là Python bất biến?Phiên bản có thể thay đổi Cách tạo các bản sao đột biếnGói bữa tiệc thrid In [1]: power_levels["Kevin"] Out[1]: 9001 In [2]: power_levels["Benny"] Out[2]: 8000 In [3]: list(power_levels.keys()) Out[3]: ['Kevin', 'Benny']8: In [1]: power_levels["Kevin"] Out[1]: 9001 In [2]: power_levels["Benny"] Out[2]: 8000 In [3]: list(power_levels.keys()) Out[3]: ['Kevin', 'Benny']4 Là một dict bất biến? In [1]: power_levels["Kevin"] Out[1]: 9001 In [2]: power_levels["Benny"] Out[2]: 8000 In [3]: list(power_levels.keys()) Out[3]: ['Kevin', 'Benny']5 Yabba-dabba-doo! Gói bữa tiệc thridLà một dict bất biến? Là một con trăn bất biến từ điển?Chúng ta có thể làm cho từ điển bất biến trong Python không?Tại sao Dict Keys là Python bất biến? Phiên bản có thể thay đổi Phiên bản bất biến In [1]: power_levels["Kevin"] Out[1]: 9001 In [2]: power_levels["Benny"] Out[2]: 8000 In [3]: list(power_levels.keys()) Out[3]: ['Kevin', 'Benny']7
??? python Là một dict bất biến?Là một con trăn bất biến từ điển?, so entries can be added, removed, and changed at any time. Là một con trăn bất biến từ điển?Chúng ta có thể làm cho từ điển bất biến trong Python không?a dictionary key must be of a type that is immutable. For example, you can use an integer, float, string, or Boolean as a dictionary key. However, neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable. Chúng ta có thể làm cho từ điển bất biến trong Python không?Tại sao Dict Keys là Python bất biến?. The object can be made mutable (or not) irrespective of what its __hash__ method does. Tại sao Dict Keys là Python bất biến?Phiên bản có thể thay đổiIf the key were a mutable object, its value could change, and thus its hash could also change. |