Độ phức tạp thời gian của từ điển trong python là gì?
Bài viết về Python này đề cập đến phương thức computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)1 của từ điển Python, cụ thể là định nghĩa, cú pháp, (các) tham số và kiểu trả về của nó. Ngoài ra, chúng tôi sẽ cung cấp một số ví dụ về cách sử dụng computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 trong thực tế. Vậy hãy bắt đầu. 🙂 Show
Python chính tả. Phương thức pop() Xem video này trên YouTube Bài viết liên quan
Sự định nghĩaPhương thức computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 vừa xóa vừa trả về một phần tử cặp (khóa-giá trị) được chỉ định từ một từ điển cú pháp
Thông số
Giá trị trả về
ví dụ 1. Khóa tồn tạiMột ví dụ về thao tác phương thức Python computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 đơn giản được áp dụng cho Từ điển Python computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items) đầu ra computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 Phương thức computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 loại bỏ toàn bộ phần tử cặp khóa-giá trị computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)22 khỏi từ điển computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)23, nhưng chỉ lưu trữ phần giá trị trong biến computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)24, đó là lý do tại sao 200. 00 đô la được trả lại khi biến laptop_item được in Khi từ điển computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)23 được in lại sau khi thao tác computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 đã được thực hiện, phần tử cặp khóa-giá trị computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)22 không còn được hiển thị trong từ điển computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)23 vì phương pháp computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 đã loại bỏ hoàn toàn phần tử khỏi từ điển ví dụ 2. Khóa không tồn tạiTiếp theo, hãy đi sâu vào một ví dụ về việc xóa một phần tử không có trong Từ điển Python. Một lần nữa, chúng tôi sử dụng computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)3 đầu ra computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)4 Như kết quả cho thấy, nếu bạn sử dụng phương pháp computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 để thử và xóa một phần tử cặp (khóa-giá trị) không có trong từ điển, Python sẽ đưa ra một computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)9 và in khóa đã thử được chuyển cho phương pháp computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 ví dụ 3. Giá trị mặc địnhVí dụ sau đặt giá trị mặc định sẽ được trả về nếu khóa được truyền cho phương thức computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 không có trong từ điển computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)9 đầu ra 0Như kết quả được in cho thấy, nếu không tìm thấy phần tử cặp (khóa-giá trị) trong từ điển bằng phương thức computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2, thì giá trị được liên kết vẫn được lưu trữ dưới dạng giá trị mặc định trong một biến đã khai báo. , trong đó biến sau này có thể được gọi để hiển thị giá trị mặc định không có trong từ điển Python Ví dụ 4. mệnh lệnh. pop() so với delVí dụ sau nêu bật sự khác biệt giữa các phương thức computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 và computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)37 được áp dụng cho từ điển Python 4đầu ra 5Như kết quả cho thấy, phương thức computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 trả về một giá trị được liên kết với khóa đã bị xóa khỏi Từ điển Python Từ khóa computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)37 của Python khác với phương thức computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 ở chỗ khi một khóa bị xóa khỏi từ điển, nó không trả về giá trị và một computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)9 được nâng lên Ví dụ 5. Xóa nhiều phímMột ví dụ về cách xóa nhiều khóa khỏi Từ điển Python bằng phương pháp computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)1 Bạn sử dụng phương pháp computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 để xóa nhiều khóa khỏi Từ điển Python
Ví dụ 6. Xóa Nếu Tồn TạiMột ví dụ về việc xóa một mục khỏi từ điển Python bằng phương thức computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2, chỉ khi mục đó tồn tại trong từ điển computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)0 Vì vậy, nếu khóa được truyền cho phương thức computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 nằm trong từ điển computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)46, giá trị khóa sẽ được trả về. Mặt khác, nếu không tìm thấy khóa được truyền cho phương thức computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)2 trong từ điển mục văn phòng, thì giá trị computer_items = {'laptop': 200.00, 'smart-phone': 300.00, 'camera': 20.00, 'desktop': 500.00} laptop_item = computer_items.pop('laptop') print('The selected laptops price is: ', laptop_item) print('Updated computer items after purchase: ', computer_items)48 sẽ được trả về cho khóa không tìm thấy trong từ điển Độ phức tạp về thời gian cho các phím dict() là gì?Độ phức tạp của nó là 0(1) trong Python 3. x. Trong Python 2. x, nó trả về một danh sách nên việc điền vào danh sách đó hoặc thực hiện một số tra cứu trên đó sẽ mất 0(n).
Từ điển như thế nào là O 1?O(1) có nghĩa là hằng số bất kể kích thước của dữ liệu . Hàm băm mất một khoảng thời gian nhất định, nhưng lượng thời gian đó không tỷ lệ với kích thước của bộ sưu tập.
Tại sao từ điển nhanh hơn danh sách?Danh sách là một tập hợp dữ liệu được sắp xếp theo thứ tự, trong khi các từ điển lưu trữ dữ liệu dưới dạng các cặp khóa-giá trị bằng cách sử dụng cấu trúc hashtable. Do đó, việc tìm nạp các phần tử từ cấu trúc dữ liệu danh sách khá phức tạp so với từ điển trong Python . Do đó, từ điển nhanh hơn danh sách trong Python.
Độ phức tạp thời gian của từ điển được sắp xếp là gì?SortedDictionary mất O(log n) thời gian để chèn và xóa mục, trong khi SortedList mất thời gian O(n) cho các thao tác tương tự. |