Độ 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. 🙂

Python chính tả. Phương thức pop[]


Xem video này trên YouTube

Bài viết liên quan

  • Phương pháp từ điển Python

Sự định nghĩa

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

dict.pop[key, default_value]

Thông số

  • 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. Khai báo khóa có phần tử cặp [khóa-giá trị] phải được trả lại và xó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]
    5. Đây là giá trị mặc định sẽ được trả về nếu khóa được chỉ định không có trong từ điển

Giá trị trả về

  • Nếu khóa đã chỉ định tồn tại trong từ điển, thì
    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ề giá trị được nhóm thành phần tử cặp [khóa-giá trị] đã xóa
  • Nếu khóa không tồn tại,
    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]
    2trả về giá trị mặc định đã chỉ định
  • Nếu khóa không có trong từ điển và giá trị mặc định không được chỉ định,
    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
    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í dụ 1. Khóa tồn tại

Mộ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ại

Tiế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 định

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

dict.pop[key, default_value]
0

Như 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 del

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

dict.pop[key, default_value]
4

đầu ra

dict.pop[key, default_value]
5

Như 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ím

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

  • Đầu tiên, một danh sách các khóa sẽ bị xóa và lưu trữ trong một biến
  • Thứ hai, khai báo một vòng lặp for lặp qua danh sách các khóa cần xóa
  • Thứ ba, áp dụng phương thức pop cho từ điển trong mỗi lần lặp, chuyển khóa lặp hiện tại từ danh sách sẽ bị xóa
  • Thứ tư, in từ điển danh tính sau thao tác vòng lặp for;

Ví dụ 6. Xóa Nếu Tồn Tại

Mộ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ự.

Chủ Đề