Làm cách nào để sử dụng pop trong từ điển python?
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 thực hiện 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, 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 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í 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 được 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 Làm thế nào là pop () khác với Popitem () trong từ điển giải thích với ví dụ?Mặc dù chúng tôi sử dụng pop() và popitem() để xóa các phần tử khỏi từ điển nhưng chúng thực sự khác nhau. pop() có thể xóa bất kỳ mục nào khỏi từ điển miễn là bạn chỉ định khóa. Mặt khác, popitem() chỉ có thể xóa và trả về giá trị của phần tử cuối cùng trong từ điển .
Sự khác biệt của Pop() trong danh sách và từ điển là gì?pop() có thể trả về cùng một giá trị như lập chỉ mục vào dict hoặc danh sách, nhưng nó cũng xóa phần tử khỏi cấu trúc dữ liệu mà phần tử sau không . |