Hướng dẫn can you edit a dictionary in python? - bạn có thể chỉnh sửa từ điển trong python không?
W3Schools được tối ưu hóa cho việc học và đào tạo. Ví dụ có thể được đơn giản hóa để cải thiện việc đọc và học tập. Hướng dẫn, tài liệu tham khảo và ví dụ được xem xét liên tục để tránh lỗi, nhưng chúng tôi không thể đảm bảo tính chính xác đầy đủ của tất cả các nội dung. Trong khi sử dụng W3Schools, bạn đồng ý đã đọc và chấp nhận các điều khoản sử dụng, cookie và chính sách bảo mật của chúng tôi. Show
Bản quyền 1999-2022 bởi dữ liệu refsnes. Đã đăng ký Bản quyền. W3Schools được cung cấp bởi W3.CSS. Chọn một thuộc tính và xem thuộc tính đó cho từng phần tử trong bảng.Cung cấp một tùy chọn để xem các biểu tượng được sắp xếp giống như bảng tuần hoàn. (dấu) Gợi ýBạn có thể sử dụng một vòng lặp để lặp qua từng phần tử. Chọn ra số hàng và số cột của các phần tử. Sử dụng hai vòng được lồng cho các vòng để in biểu tượng của phần tử hoặc một loạt khoảng trống, tùy thuộc vào mức độ đầy đủ đó. Bạn có thể thay đổi dữ liệu trong một python từ điển không?Trong trường hợp bạn cần một giải pháp khai báo, bạn có thể sử dụng Dict.Update () để thay đổi các giá trị trong một dict. dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3} Là một từ điển Python có thể sửa đổi? dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3, } Điều này dễ đọc hơn một chút, đặc biệt là nếu các giá trị dài. Thí dụ¶Một ví dụ đơn giản liên quan đến việc mô hình hóa một từ điển thực tế. python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } Chúng ta có thể đưa các mục riêng lẻ ra khỏi từ điển, bằng cách đặt tên của từ điển và chìa khóa trong dấu ngoặc vuông: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } print("\nWord: %s" % 'list') print("Meaning: %s" % python_words['list']) print("\nWord: %s" % 'dictionary') print("Meaning: %s" % python_words['dictionary']) print("\nWord: %s" % 'function') print("Meaning: %s" % python_words['function']) Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Word: function Meaning: A named set of instructions that defines a set of actions in Python. Mã này trông khá lặp đi lặp lại, và nó là. Từ điển có cú pháp cho vòng lặp riêng của chúng, nhưng vì có hai loại thông tin trong từ điển, cấu trúc này phức tạp hơn một chút so với danh sách. Dưới đây là cách sử dụng một vòng lặp cho một từ điển: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning) Word: function Meaning: A named set of instructions that defines a set of actions in Python. Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Đầu ra là giống hệt nhau, nhưng chúng tôi đã thực hiện nó trong 3 dòng thay vì 6. Nếu chúng tôi có 100 thuật ngữ trong từ điển, chúng tôi vẫn có thể in chúng ra chỉ với 3 dòng. Phần khó khăn duy nhất về việc sử dụng cho các vòng lặp với từ điển là tìm ra những gì sẽ gọi hai biến đầu tiên đó. Cú pháp chung cho điều này cho vòng lặp là: for key_name, value_name in dictionary_name.items(): print(key_name) # The key is stored in whatever you called the first variable. print(value_name) # The value associated with that key is stored in your second variable. Bài tậpTên vật nuôi¶
Bỏ phiếu cho Friends¶
Các hoạt động chung với từ điểnCó một vài điều phổ biến bạn sẽ muốn làm với từ điển. Chúng bao gồm thêm các cặp giá trị khóa mới, sửa đổi thông tin trong từ điển và xóa các mục khỏi từ điển. Thêm các cặp giá trị khóa mớiĐể thêm một cặp giá trị khóa mới, bạn đặt tên từ điển theo sau là khóa mới trong dấu ngoặc vuông và đặt bằng giá trị mới. Chúng tôi sẽ hiển thị điều này bằng cách bắt đầu với một từ điển trống và tạo lại từ điển từ ví dụ trên. ________số 8 Word: function Meaning: A named set of instructions that defines a set of actions in Python. Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Sửa đổi các giá trị trong từ điểnTại một số điểm, bạn có thể muốn sửa đổi một trong các giá trị trong từ điển của mình. Việc sửa đổi một giá trị trong từ điển khá giống với việc sửa đổi một phần tử trong danh sách. Bạn đặt tên của từ điển và sau đó là khóa trong dấu ngoặc vuông và đặt bằng giá trị mới. dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3, }0 dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3, }1 Xóa các cặp giá trị khóaBạn có thể muốn xóa một số cặp giá trị khóa khỏi một trong những từ điển của bạn tại một số điểm. Bạn có thể làm điều này bằng cách sử dụng cùng một lệnh for key_name, value_name in dictionary_name.items(): print(key_name) # The key is stored in whatever you called the first variable. print(value_name) # The value associated with that key is stored in your second variable.0 bạn đã học cách sử dụng với các danh sách. Để xóa một cặp giá trị khóa, bạn đưa ra lệnh for key_name, value_name in dictionary_name.items(): print(key_name) # The key is stored in whatever you called the first variable. print(value_name) # The value associated with that key is stored in your second variable.0, theo sau là tên của từ điển, với khóa mà bạn muốn xóa. Điều này loại bỏ khóa và giá trị như một cặp. dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3, }2 dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3, }3 Nếu bạn định làm việc với mã này, bạn chắc chắn sẽ muốn đặt mã để hiển thị từ điển vào một hàm. Hãy xem cái này trông như thế nào: dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3, }4 dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3, }3 Miễn là chúng ta có một chức năng sạch đẹp để làm việc, hãy làm sạch đầu ra của chúng ta một chút: dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3, }6 dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3, }7 Đây là mã thực tế hơn nhiều. Sửa đổi các khóa trong từ điểnViệc sửa đổi một giá trị trong từ điển là đơn giản, bởi vì không có gì khác phụ thuộc vào giá trị. Sửa đổi một khóa khó hơn một chút, bởi vì mỗi khóa được sử dụng để mở khóa giá trị. Chúng ta có thể thay đổi một khóa trong hai bước:
Đây là những gì nó trông như thế nào. Chúng tôi sẽ sử dụng một từ điển chỉ với một cặp giá trị khóa, để giữ cho mọi thứ đơn giản. dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3, }8 dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3, }9 Bài tậpTên vật nuôi¶
Hãy nghĩ về một câu hỏi bạn có thể hỏi bạn bè của bạn. Tạo một từ điển trong đó mỗi khóa là tên của một người và mỗi giá trị là câu trả lời của người đó cho câu hỏi của bạn.
Lặp qua một từ điển thánhVì từ điển thực sự là về việc kết nối các bit thông tin, bạn sẽ thường sử dụng chúng theo cách được mô tả ở trên, nơi bạn thêm các cặp giá trị khóa bất cứ khi nào bạn nhận được một số thông tin mới và sau đó bạn lấy các cặp giá trị khóa mà bạn quan tâm. Tuy nhiên, đôi khi, bạn sẽ muốn lặp qua toàn bộ từ điển. Có nhiều hướng khác nhau để làm điều đó:
Vòng lặp qua tất cả các cặp giá trị khóaĐây là loại vòng lặp được hiển thị trong ví dụ đầu tiên. Đây là vòng lặp này trông như thế nào, ở định dạng chung: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', }0 python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', }1 Điều này hoạt động bởi vì phương thức for key_name, value_name in dictionary_name.items(): print(key_name) # The key is stored in whatever you called the first variable. print(value_name) # The value associated with that key is stored in your second variable.2 kéo tất cả các cặp giá trị khóa từ một từ điển vào một danh sách các bộ dữ liệu: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', }2 python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', }3 Cú pháp for key_name, value_name in dictionary_name.items(): print(key_name) # The key is stored in whatever you called the first variable. print(value_name) # The value associated with that key is stored in your second variable.3 thực hiện công việc lặp đi qua danh sách các bộ dữ liệu này và kéo mục thứ nhất và thứ hai từ mỗi tuple cho chúng tôi. Không có gì đặc biệt về bất kỳ tên biến nào trong số này, vì vậy mã Python sử dụng cú pháp này trở nên thực sự dễ đọc. Thay vì tạo một ví dụ mới về vòng lặp này, chúng ta hãy xem xét một lần nữa ví dụ ban đầu để thấy điều này trong một bối cảnh có ý nghĩa: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', }4 Word: function Meaning: A named set of instructions that defines a set of actions in Python. Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Vòng qua tất cả các phím trong Từ điển OrPython cung cấp một cú pháp rõ ràng để lặp qua chỉ các phím trong từ điển: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', }6 python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', }7 Đây thực sự là hành vi mặc định của việc lặp qua từ điển. Vì vậy, bạn có thể bỏ qua phần for key_name, value_name in dictionary_name.items(): print(key_name) # The key is stored in whatever you called the first variable. print(value_name) # The value associated with that key is stored in your second variable.4 và nhận được chính xác hành vi tương tự: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', }8
python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', }7 Ưu điểm duy nhất của việc sử dụng for key_name, value_name in dictionary_name.items(): print(key_name) # The key is stored in whatever you called the first variable. print(value_name) # The value associated with that key is stored in your second variable.4 trong mã là một chút rõ ràng. Nhưng bất cứ ai biết Python một cách hợp lý sẽ nhận ra phiên bản thứ hai làm gì. Trong phần còn lại của mã của chúng tôi, chúng tôi sẽ rời khỏi for key_name, value_name in dictionary_name.items(): print(key_name) # The key is stored in whatever you called the first variable. print(value_name) # The value associated with that key is stored in your second variable.4 khi chúng tôi muốn hành vi này. Bạn có thể rút ra giá trị của bất kỳ khóa nào mà bạn quan tâm trong vòng lặp của mình, sử dụng ký hiệu tiêu chuẩn để truy cập giá trị từ điển từ khóa: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } print("\nWord: %s" % 'list') print("Meaning: %s" % python_words['list']) print("\nWord: %s" % 'dictionary') print("Meaning: %s" % python_words['dictionary']) print("\nWord: %s" % 'function') print("Meaning: %s" % python_words['function'])0 python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } print("\nWord: %s" % 'list') print("Meaning: %s" % python_words['list']) print("\nWord: %s" % 'dictionary') print("Meaning: %s" % python_words['dictionary']) print("\nWord: %s" % 'function') print("Meaning: %s" % python_words['function'])1 Chúng ta hãy chỉ ra cách chúng ta có thể sử dụng điều này trong chương trình Python Words của chúng ta. Loại vòng lặp này cung cấp một cách đơn giản để chỉ hiển thị các từ trong từ điển: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } print("\nWord: %s" % 'list') print("Meaning: %s" % python_words['list']) print("\nWord: %s" % 'dictionary') print("Meaning: %s" % python_words['dictionary']) print("\nWord: %s" % 'function') print("Meaning: %s" % python_words['function'])2 python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } print("\nWord: %s" % 'list') print("Meaning: %s" % python_words['list']) print("\nWord: %s" % 'dictionary') print("Meaning: %s" % python_words['dictionary']) print("\nWord: %s" % 'function') print("Meaning: %s" % python_words['function'])3 Chúng tôi có thể mở rộng điều này một chút để tạo một chương trình cho phép bạn tìm kiếm từ. Trước tiên chúng tôi để người dùng chọn một từ. Khi người dùng đã chọn một từ, chúng tôi sẽ nhận được nghĩa cho từ đó và hiển thị nó: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } print("\nWord: %s" % 'list') print("Meaning: %s" % python_words['list']) print("\nWord: %s" % 'dictionary') print("Meaning: %s" % python_words['dictionary']) print("\nWord: %s" % 'function') print("Meaning: %s" % python_words['function'])4 python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } print("\nWord: %s" % 'list') print("Meaning: %s" % python_words['list']) print("\nWord: %s" % 'dictionary') print("Meaning: %s" % python_words['dictionary']) print("\nWord: %s" % 'function') print("Meaning: %s" % python_words['function'])5 Điều này cho phép người dùng chọn một từ đã được xác định. Nếu chúng ta đặt phần đầu vào của chương trình trong một vòng lặp trong một thời gian, người dùng có thể thấy bao nhiêu định nghĩa như họ muốn: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } print("\nWord: %s" % 'list') print("Meaning: %s" % python_words['list']) print("\nWord: %s" % 'dictionary') print("Meaning: %s" % python_words['dictionary']) print("\nWord: %s" % 'function') print("Meaning: %s" % python_words['function'])6 python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } print("\nWord: %s" % 'list') print("Meaning: %s" % python_words['list']) print("\nWord: %s" % 'dictionary') print("Meaning: %s" % python_words['dictionary']) print("\nWord: %s" % 'function') print("Meaning: %s" % python_words['function'])7 Điều này cho phép người dùng yêu cầu bao nhiêu ý nghĩa như họ muốn, nhưng nó lấy từ "bỏ" làm từ được yêu cầu. Hãy thêm một điều khoản for key_name, value_name in dictionary_name.items(): print(key_name) # The key is stored in whatever you called the first variable. print(value_name) # The value associated with that key is stored in your second variable.7 để làm sạch hành vi này: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } print("\nWord: %s" % 'list') print("Meaning: %s" % python_words['list']) print("\nWord: %s" % 'dictionary') print("Meaning: %s" % python_words['dictionary']) print("\nWord: %s" % 'function') print("Meaning: %s" % python_words['function'])8 python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } print("\nWord: %s" % 'list') print("Meaning: %s" % python_words['list']) print("\nWord: %s" % 'dictionary') print("Meaning: %s" % python_words['dictionary']) print("\nWord: %s" % 'function') print("Meaning: %s" % python_words['function'])9 Vòng lặp qua tất cả các giá trị trong một từ điển thánhPython cung cấp một cú pháp đơn giản để lặp qua tất cả các giá trị trong từ điển, cũng như: Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Word: function Meaning: A named set of instructions that defines a set of actions in Python.0 Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Word: function Meaning: A named set of instructions that defines a set of actions in Python.1 Chúng ta có thể sử dụng cú pháp vòng lặp này để có một chút niềm vui với ví dụ từ điển, bằng cách thực hiện một chương trình đố nhỏ. Chương trình sẽ hiển thị một ý nghĩa và yêu cầu người dùng đoán từ phù hợp với ý nghĩa đó. Hãy bắt đầu bằng cách hiển thị tất cả các ý nghĩa trong từ điển: Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Word: function Meaning: A named set of instructions that defines a set of actions in Python.2 Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Word: function Meaning: A named set of instructions that defines a set of actions in Python.3 Bây giờ chúng tôi có thể thêm một lời nhắc sau mỗi ý nghĩa, yêu cầu người dùng đoán từ: Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Word: function Meaning: A named set of instructions that defines a set of actions in Python.4 Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Word: function Meaning: A named set of instructions that defines a set of actions in Python.5 Điều này đang bắt đầu hoạt động, nhưng chúng ta có thể thấy từ đầu ra rằng người dùng không có cơ hội dự đoán thứ hai nếu họ đoán sai cho bất kỳ ý nghĩa nào. Chúng ta có thể sử dụng một vòng lặp trong thời gian xung quanh mã đoán, để cho người dùng đoán cho đến khi họ hiểu đúng: Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Word: function Meaning: A named set of instructions that defines a set of actions in Python.6 Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Word: function Meaning: A named set of instructions that defines a set of actions in Python.7 Thế này tốt hơn. Bây giờ, nếu dự đoán không chính xác, người dùng sẽ bị bắt trong một vòng lặp rằng họ chỉ có thể thoát bằng cách đoán chính xác. Bản sửa đổi cuối cùng cho mã này là hiển thị cho người dùng một danh sách các từ để chọn khi họ được yêu cầu đoán: Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Word: function Meaning: A named set of instructions that defines a set of actions in Python.8 Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs. Word: function Meaning: A named set of instructions that defines a set of actions in Python.9 Lặp qua một từ điển theo thứ tựTừ điển khá hữu ích vì chúng cho phép các bit thông tin được kết nối. Tuy nhiên, một trong những vấn đề với từ điển là chúng không được lưu trữ theo bất kỳ thứ tự cụ thể nào. Khi bạn lấy tất cả các khóa hoặc giá trị trong từ điển của bạn, bạn không thể chắc chắn rằng bạn sẽ lấy lại thứ tự nào. Có một cách nhanh chóng và dễ dàng để làm điều này, tuy nhiên, khi bạn muốn chúng theo một thứ tự cụ thể. Chúng ta hãy xem thứ tự kết quả từ một cuộc gọi đơn giản đến từ điển.Keys (): python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning)0 Danh sách kết quả không theo thứ tự. Danh sách các khóa có thể được đặt theo thứ tự bằng cách chuyển danh sách vào hàm Sắp xếp (), trong dòng khởi tạo vòng lặp cho vòng lặp: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning)1 Cách tiếp cận này có thể được sử dụng để làm việc với các khóa và giá trị theo thứ tự. Ví dụ: các từ và ý nghĩa có thể được in theo thứ tự bảng chữ cái theo từ: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning)2 python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning)3 Trong ví dụ này, các khóa đã được đặt vào thứ tự bảng chữ cái chỉ trong vòng lặp cho vòng lặp; Python đã không thay đổi cách lưu trữ từ điển. Vì vậy, lần tới khi từ điển được truy cập, các khóa có thể được trả lại theo bất kỳ thứ tự nào. Không có cách nào để chỉ định vĩnh viễn một đơn đặt hàng cho các mục trong một từ điển thông thường, nhưng nếu bạn muốn làm điều này, bạn có thể sử dụng cấu trúc đặt hàng. Bài tậpCao nguyên núi
Chiều cao núi 2¶
Làm làm tổNesting là một trong những khái niệm mạnh mẽ nhất mà chúng ta đã đến cho đến nay. Nesting liên quan đến việc đặt một danh sách hoặc từ điển trong một danh sách hoặc từ điển khác. Chúng tôi sẽ xem xét hai ví dụ ở đây, danh sách bên trong một từ điển và từ điển bên trong một từ điển. Với việc làm tổ, loại thông tin chúng tôi có thể mô hình hóa trong các chương trình của chúng tôi được mở rộng rất nhiều. Danh sách trong Dictionary¶Một từ điển kết nối hai mẩu thông tin. Hai mẩu thông tin đó có thể là bất kỳ loại cấu trúc dữ liệu nào trong Python. Hãy tiếp tục sử dụng chuỗi cho các khóa của chúng tôi, nhưng hãy thử đưa ra một danh sách làm giá trị. Ví dụ đầu tiên sẽ liên quan đến việc lưu trữ một số số yêu thích của mọi người. Các khóa bao gồm tên của mọi người và các giá trị là danh sách các số yêu thích của mỗi người. Trong ví dụ đầu tiên này, chúng tôi sẽ truy cập mỗi lần một danh sách của mỗi người. python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning)4 python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning)5 Chúng tôi thực sự chỉ đang làm việc theo từng khóa trong từ điển, vì vậy hãy sử dụng một vòng lặp để đi qua các phím trong từ điển: python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning)6 python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning)7 Cấu trúc này khá phức tạp, vì vậy đừng lo lắng nếu mất một thời gian để mọi thứ chìm vào. Từ điển có lẽ có ý nghĩa; Mỗi người được kết nối với một danh sách các số yêu thích của họ. Điều này hoạt động, nhưng chúng tôi không muốn in Python thô trong đầu ra của chúng tôi. Chúng ta hãy sử dụng một vòng lặp để in các số yêu thích riêng lẻ, thay vì trong danh sách Python. python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning)8 python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning)9 Mọi thứ trở nên phức tạp hơn một chút bên trong vòng lặp. Giá trị là một danh sách các số yêu thích, vì vậy vòng lặp cho mỗi lần yêu thích ra khỏi danh sách một lần. Nếu nó có ý nghĩa hơn với bạn, bạn có thể tự do lưu trữ danh sách trong một biến mới và sử dụng nó để xác định vòng lặp cho vòng lặp của bạn: Word: function Meaning: A named set of instructions that defines a set of actions in Python. Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs.0 python_words = {'list': 'A collection of values that are not connected, but have an order.', 'dictionary': 'A collection of key-value pairs.', 'function': 'A named set of instructions that defines a set of actions in Python.', } # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning)9 Từ điển trong Từ điểnKhái niệm làm tổ mạnh mẽ nhất mà chúng ta sẽ đề cập ngay bây giờ là làm tổ trong một từ điển bên trong một từ điển. Để chứng minh điều này, hãy tạo ra một từ điển vật nuôi, với một số thông tin về mỗi thú cưng. Các chìa khóa cho từ điển này sẽ bao gồm tên của thú cưng. Các giá trị sẽ bao gồm thông tin như loại động vật, chủ sở hữu và liệu thú cưng đã được tiêm phòng. Word: function Meaning: A named set of instructions that defines a set of actions in Python. Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs.2 Word: function Meaning: A named set of instructions that defines a set of actions in Python. Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs.3 Rõ ràng đây là một số mã lặp đi lặp lại, nhưng nó cho thấy chính xác cách chúng tôi truy cập thông tin trong một từ điển lồng nhau. Trong bộ báo cáo for key_name, value_name in dictionary_name.items(): print(key_name) # The key is stored in whatever you called the first variable. print(value_name) # The value associated with that key is stored in your second variable.8 đầu tiên, chúng tôi sử dụng tên 'Willie' để mở khóa 'loại động vật' của anh ấy, 'chủ sở hữu' mà anh ấy có, và anh ấy có 'tiêm vắc -xin' hay không. Chúng ta phải bọc giá trị tiêm chủng trong hàm for key_name, value_name in dictionary_name.items(): print(key_name) # The key is stored in whatever you called the first variable. print(value_name) # The value associated with that key is stored in your second variable.9 để Python biết chúng ta muốn các từ 'thật' và 'sai', chứ không phải các giá trị # Create an empty dictionary. python_words = {} # Fill the dictionary, pair by pair. python_words['list'] ='A collection of values that are not connected, but have an order.' python_words['dictionary'] = 'A collection of key-value pairs.' python_words['function'] = 'A named set of instructions that defines a set of actions in Python.' # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning)0 và # Create an empty dictionary. python_words = {} # Fill the dictionary, pair by pair. python_words['list'] ='A collection of values that are not connected, but have an order.' python_words['dictionary'] = 'A collection of key-value pairs.' python_words['function'] = 'A named set of instructions that defines a set of actions in Python.' # Print out the items in the dictionary. for word, meaning in python_words.items(): print("\nWord: %s" % word) print("Meaning: %s" % meaning)1. Sau đó chúng tôi làm điều tương tự cho mỗi con vật. Hãy viết lại chương trình này, sử dụng một vòng lặp cho các khóa của từ điển: Word: function Meaning: A named set of instructions that defines a set of actions in Python. Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs.4 Word: function Meaning: A named set of instructions that defines a set of actions in Python. Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs.5 Mã này ngắn hơn và dễ bảo trì hơn nhiều. Nhưng ngay cả mã này sẽ không theo kịp từ điển của chúng tôi. Nếu chúng tôi thêm thông tin vào từ điển sau này, chúng tôi sẽ phải cập nhật các câu lệnh in của mình. Hãy đặt một vòng thứ hai cho vòng lặp bên trong vòng lặp đầu tiên để chạy qua tất cả các thông tin về từng con vật cưng: Word: function Meaning: A named set of instructions that defines a set of actions in Python. Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs.6 Word: function Meaning: A named set of instructions that defines a set of actions in Python. Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs.7 Vòng lặp lồng nhau này có thể trông khá phức tạp, vì vậy một lần nữa, đừng lo lắng nếu nó không có ý nghĩa trong một thời gian.
Hãy hiển thị một phiên bản cuối cùng sử dụng một số câu lệnh IF để làm sạch dữ liệu của chúng tôi để in: Word: function Meaning: A named set of instructions that defines a set of actions in Python. Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs.8 Word: function Meaning: A named set of instructions that defines a set of actions in Python. Word: list Meaning: A collection of values that are not connected, but have an order. Word: dictionary Meaning: A collection of key-value pairs.9 Mã này dài hơn rất nhiều, và bây giờ chúng tôi đã lồng nhau nếu các câu lệnh cũng như lồng nhau cho các vòng lặp. Nhưng hãy nhớ rằng, cấu trúc này sẽ hoạt động nếu có 1000 vật nuôi trong từ điển của chúng tôi và nó sẽ hoạt động nếu chúng tôi lưu trữ 1000 mẩu thông tin về mỗi thú cưng. Một cấp độ làm tổ cho phép chúng tôi mô hình hóa một loạt thông tin đáng kinh ngạc. Một lưu ý quan trọng về Nesting¶Trong khi một cấp độ làm tổ thực sự hữu ích, thì làm tổ sâu hơn nhiều so với điều đó thực sự phức tạp, thực sự nhanh chóng. Có các cấu trúc khác như các lớp có thể hữu ích hơn cho việc mô hình hóa thông tin. Ngoài ra, chúng ta có thể sử dụng Python để lưu trữ thông tin trong cơ sở dữ liệu, đây là công cụ thích hợp để lưu trữ thông tin được lồng sâu. Thông thường khi bạn đang lưu trữ thông tin trong cơ sở dữ liệu, bạn sẽ rút một bộ thông tin nhỏ đó ra và đưa nó vào một từ điển, hoặc một cấu trúc hơi lồng nhau, và sau đó làm việc với nó. Nhưng bạn sẽ hiếm khi, nếu có, làm việc với các cấu trúc dữ liệu Python được lồng nhiều hơn một cấp độ sâu. Bài tậpChiều cao núi 3¶
Chiều cao núi 4¶
Thử thách tổng thểBưc tương tư ngư¶
Ứng dụng bảng tuần hoàn
Cho phép người dùng phân loại các từ.Ứng dụng bảng tuần hoàn
Biểu tượng, tên, số nguyên tử, hàng và cộtChọn ít nhất một phần thông tin khác để đưa vào ứng dụng của bạn.use dict. update() to change values in a dict.
Cung cấp menu tùy chọn cho người dùng:Cách cập nhật từ điển trong Python.Từ điển có thể thay đổi, có nghĩa là chúng có thể được sửa đổi.Chúng ta có thể thêm một cặp {key: value} mới hoặc sửa đổi các cặp hiện có.Dictionaries are mutable, which means they can be modified. We can add a new {key:value} pair or modify existing ones. |