Từ điển độ phức tạp thời gian Python

Trong Python, các toán tử

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 và
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
3 kiểm tra tư cách thành viên trong danh sách, bộ dữ liệu, từ điển, v.v.

Show

Bài viết này mô tả các nội dung sau

  • Cách sử dụng toán tử
    l = [0, 1, 2]
    i = 0
    
    if i in l:
        print(f'{i} is a member of {l}.')
    else:
        print(f'{i} is not a member of {l}.')
    # 0 is a member of [0, 1, 2].
    
    2
    • sử dụng cơ bản
    • Kiểm tra xem chúng có bằng nhau về giá trị không
    • Với câu lệnh
      l = [0, 1, 2]
      i = 0
      
      if i in l:
          print(f'{i} is a member of {l}.')
      else:
          print(f'{i} is not a member of {l}.')
      # 0 is a member of [0, 1, 2].
      
      5
    • l = [0, 1, 2]
      i = 0
      
      if i in l:
          print(f'{i} is a member of {l}.')
      else:
          print(f'{i} is not a member of {l}.')
      # 0 is a member of [0, 1, 2].
      
      2 cho từ điển (
      l = [0, 1, 2]
      i = 0
      
      if i in l:
          print(f'{i} is a member of {l}.')
      else:
          print(f'{i} is not a member of {l}.')
      # 0 is a member of [0, 1, 2].
      
      7)
    • l = [0, 1, 2]
      i = 0
      
      if i in l:
          print(f'{i} is a member of {l}.')
      else:
          print(f'{i} is not a member of {l}.')
      # 0 is a member of [0, 1, 2].
      
      2 cho chuỗi (______09)
  • l = [0, 1, 2]
    i = 0
    
    if i in l:
        print(f'{i} is a member of {l}.')
    else:
        print(f'{i} is not a member of {l}.')
    # 0 is a member of [0, 1, 2].
    
    3 (phủ định của
    l = [0, 1, 2]
    i = 0
    
    if i in l:
        print(f'{i} is a member of {l}.')
    else:
        print(f'{i} is not a member of {l}.')
    # 0 is a member of [0, 1, 2].
    
    2)
  • l = [0, 1, 2]
    i = 0
    
    if i in l:
        print(f'{i} is a member of {l}.')
    else:
        print(f'{i} is not a member of {l}.')
    # 0 is a member of [0, 1, 2].
    
    2 cho nhiều yếu tố
    • Sử dụng
      l = [0, 1, 2]
      i = 0
      
      if i in l:
          print(f'{i} is a member of {l}.')
      else:
          print(f'{i} is not a member of {l}.')
      # 0 is a member of [0, 1, 2].
      
      13,
      l = [0, 1, 2]
      i = 0
      
      if i in l:
          print(f'{i} is a member of {l}.')
      else:
          print(f'{i} is not a member of {l}.')
      # 0 is a member of [0, 1, 2].
      
      14
    • Sử dụng
      l = [0, 1, 2]
      i = 0
      
      if i in l:
          print(f'{i} is a member of {l}.')
      else:
          print(f'{i} is not a member of {l}.')
      # 0 is a member of [0, 1, 2].
      
      15
  • Độ phức tạp thời gian của
    l = [0, 1, 2]
    i = 0
    
    if i in l:
        print(f'{i} is a member of {l}.')
    else:
        print(f'{i} is not a member of {l}.')
    # 0 is a member of [0, 1, 2].
    
    2
    • Chậm cho danh sách.
      l = [0, 1, 2]
      i = 0
      
      if i in l:
          print(f'{i} is a member of {l}.')
      else:
          print(f'{i} is not a member of {l}.')
      # 0 is a member of [0, 1, 2].
      
      17
    • Nhanh chóng cho bộ.
      l = [0, 1, 2]
      i = 0
      
      if i in l:
          print(f'{i} is a member of {l}.')
      else:
          print(f'{i} is not a member of {l}.')
      # 0 is a member of [0, 1, 2].
      
      18
    • cho từ điển
  • l = [0, 1, 2]
    i = 0
    
    if i in l:
        print(f'{i} is a member of {l}.')
    else:
        print(f'{i} is not a member of {l}.')
    # 0 is a member of [0, 1, 2].
    
    2 trong câu lệnh
    l = [0, 1, 2]
    i = 0
    
    if i in l:
        print(f'{i} is a member of {l}.')
    else:
        print(f'{i} is not a member of {l}.')
    # 0 is a member of [0, 1, 2].
    
    80 và cách hiểu danh sách

Từ

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 cũng được sử dụng trong câu lệnh
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
80 và cách hiểu danh sách. Xem các bài viết sau để biết chi tiết

  • vòng lặp for trong Python (với phạm vi, liệt kê, zip, v.v. )
  • Danh sách hiểu trong Python

Liên kết được tài trợ

Cách sử dụng toán tử l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 2

sử dụng cơ bản

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
84 trả về
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
85 nếu
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
86 được bao gồm trong
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
87 và
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
88 nếu không

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
7

nguồn.

Không chỉ

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
89, mà cả
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
90,
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
15,
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
92 và các đối tượng có thể lặp lại khác đều có thể được vận hành

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2

nguồn.

Từ điển (

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
7) và chuỗi (
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
9) được mô tả sau

Kiểm tra xem chúng có bằng nhau về giá trị không

Thử nghiệm của

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 dựa trên việc các giá trị có bằng nhau hay không cũng như
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
96. Đó là
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
85 nếu giá trị bằng nhau ngay cả khi loại khác nhau

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
8

nguồn.

Lưu ý rằng

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
98 là một lớp con của số nguyên
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
99, vì vậy
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
85 và
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
88 tương ứng với
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
32 và
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
33

  • Chuyển đổi bool (True, False) và các loại khác với nhau trong Python

Với câu lệnh l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 5

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 trả về một giá trị bool (
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
85,
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
88) và có thể được sử dụng trực tiếp trong câu lệnh
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
5

  • câu lệnh if trong Python (if, elif, other)

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].

nguồn.

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
1

nguồn.

Lưu ý rằng danh sách, bộ dữ liệu, chuỗi, v.v. được đánh giá là

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
88 nếu chúng trống và
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
85 nếu chúng không. Nếu bạn muốn kiểm tra xem một đối tượng có rỗng hay không, bạn có thể sử dụng đối tượng as it is

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
8

nguồn.

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
9

nguồn.

Xem thêm các bài viết sau để kiểm tra giá trị thực cho từng loại

  • Chuyển đổi bool (True, False) và các loại khác với nhau trong Python

l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 2 cho từ điển (l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 7)

Thao tác

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 cho từ điển (
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
7) kiểm tra trên khóa

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
3

nguồn.

Sử dụng

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
15,
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
16 nếu bạn muốn kiểm tra giá trị hoặc cặp khóa-giá trị

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
1

nguồn.

Xem chi tiết bài viết sau

  • Kiểm tra xem khóa/giá trị có tồn tại trong từ điển bằng Python không

l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 2 cho chuỗi (______09)

Phép toán

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 cho chuỗi (
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
9) kiểm tra sự tồn tại của chuỗi con

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
8

nguồn.

Xem bài viết sau về tìm kiếm chuỗi. Nó cũng mô tả các hoạt động sử dụng các biểu thức thông thường

  • Tìm kiếm một chuỗi trong Python (Kiểm tra xem có bao gồm chuỗi con không/Nhận vị trí chuỗi con)

l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 3 (phủ định của l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 2)

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
83 trả về phủ định của
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
84

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
20

nguồn.

Kết quả tương tự được trả về bằng cách thêm

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
85 vào toàn bộ hoạt động của
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
21

nguồn.

Tuy nhiên, nếu bạn thêm

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
85 vào toàn bộ hoạt động của
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2, nó sẽ được hiểu theo hai cách, như hình bên dưới, vì vậy bạn nên sử dụng rõ ràng hơn
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
3

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
22

nguồn.

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 có mức độ ưu tiên cao hơn so với
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
85, nên nó được coi là thứ nhất nếu không có dấu ngoặc đơn

Trường hợp thứ hai được công nhận như sau

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
23

nguồn.

l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 2 cho nhiều yếu tố

Nếu bạn muốn kiểm tra xem có bao gồm nhiều phần tử hay không, sử dụng danh sách các phần tử đó như sau sẽ không hoạt động. Nó sẽ được kiểm tra xem bản thân danh sách có được đưa vào hay không

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
24

nguồn.

Sử dụng

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
13,
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
14 hoặc bộ

Sử dụng l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 13, l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 14

Kết hợp nhiều hoạt động của

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 bằng cách sử dụng
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
13 và
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
14. Nó sẽ được kiểm tra xem cả hai hay một trong hai được bao gồm

  • Toán tử Boolean trong Python (and, or, not)

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
25

nguồn.

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 và
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
3 có độ ưu tiên cao hơn so với
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
13 và
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
14, dấu ngoặc đơn là không cần thiết. Tất nhiên nếu khó đọc bạn có thể đặt trong ngoặc đơn như ví dụ vừa rồi

Sử dụng l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 15

Nếu bạn có nhiều phần tử muốn kiểm tra, sẽ dễ dàng hơn khi sử dụng tập hợp ________ 113, ________ 114

  • Đặt các hoạt động trong Python (hợp, giao, chênh lệch đối xứng, v.v. )

Ví dụ: việc

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
217 có chứa tất cả các phần tử của
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
218 hay không tương đương với việc
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
218 có phải là tập con của
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
217 hay không

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
26

nguồn.

Việc

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
217 không chứa các phần tử của
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
218 có tương đương với việc
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
217 và
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
218 có nguyên tố cùng nhau hay không

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
27

nguồn.

Nếu

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
217 và
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
218 không nguyên tố cùng nhau, điều đó có nghĩa là
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
217 chứa ít nhất một phần tử của
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
218

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
28

nguồn.

Liên kết được tài trợ

Độ phức tạp thời gian của l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 2

Tốc độ thực thi của toán tử

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 phụ thuộc vào loại đối tượng đích

Kết quả đo thời gian thực hiện của

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 cho các danh sách, bộ và từ điển được hiển thị bên dưới

Lưu ý rằng mã bên dưới sử dụng lệnh ma thuật Jupyter Notebook

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
232 và không hoạt động khi chạy dưới dạng tập lệnh Python

  • Đo thời gian thực hiện với timeit trong Python

Xem bài viết sau để biết độ phức tạp của thời gian

  • Độ phức tạp của thời gian - Python Wiki

Lấy danh sách 10 phần tử và 10000 phần tử làm ví dụ

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
29

nguồn.

Mã mẫu bên dưới được thực thi bằng Python 3. 7. 4, và tất nhiên, kết quả có thể thay đổi tùy theo môi trường

Chậm cho danh sách. Trên)

Độ phức tạp thời gian trung bình của toán tử

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 cho danh sách là
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
17. Nó trở nên chậm hơn khi có nhiều yếu tố

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
80

nguồn.

Thời gian thực hiện rất khác nhau tùy thuộc vào vị trí của giá trị cần tìm. Mất nhiều thời gian nhất khi giá trị của nó ở cuối hoặc không tồn tại

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
81

nguồn.

Nhanh chóng cho bộ. Ô(1)

Độ phức tạp thời gian trung bình của toán tử

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 cho các tập hợp là
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
18. không phụ thuộc vào số phần tử

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
82

nguồn.

Thời gian thực hiện không thay đổi tùy theo giá trị cần tìm

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
83

nguồn.

Nếu bạn muốn lặp lại thao tác

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 cho một danh sách có nhiều phần tử, thì chuyển đổi trước thành tập hợp sẽ nhanh hơn

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
84

nguồn.

Lưu ý rằng cần có thời gian để chuyển đổi một danh sách thành một tập hợp, do đó, có thể giữ danh sách đó dưới dạng danh sách nhanh hơn nếu số lượng thao tác

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 nhỏ

cho từ điển

Lấy từ điển sau đây làm ví dụ

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
85

nguồn.

Như đã đề cập ở trên, hoạt động

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 cho từ điển kiểm tra các phím

Khóa của từ điển là một giá trị duy nhất cũng như tập hợp và thời gian thực hiện gần giống như đối với tập hợp

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
86

nguồn.

Mặt khác, các giá trị từ điển được phép sao chép giống như một danh sách. Thời gian thực hiện của

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 cho
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
15 gần giống như đối với danh sách

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
87

nguồn.

Các cặp khóa-giá trị là duy nhất. Thời gian thực hiện của

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 cho
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
16 là khoảng
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
15 + α

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
88

nguồn.

l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 2 trong câu lệnh l = [0, 1, 2] i = 0 if i in l: print(f'{i} is a member of {l}.') else: print(f'{i} is not a member of {l}.') # 0 is a member of [0, 1, 2]. 80 và cách hiểu danh sách

Từ

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 cũng được sử dụng trong câu lệnh
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
80 và cách hiểu danh sách

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
89

nguồn.

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
0

nguồn.

Xem các bài viết sau để biết chi tiết về câu lệnh

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
80 và cách hiểu danh sách

  • vòng lặp for trong Python (với phạm vi, liệt kê, zip, v.v. )
  • Danh sách hiểu trong Python

Lưu ý rằng toán tử

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 có thể được sử dụng làm điều kiện trong việc hiểu danh sách, điều này gây nhầm lẫn

  • Trích xuất và thay thế các phần tử đáp ứng điều kiện của danh sách chuỗi trong Python

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
1

nguồn.

l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 đầu tiên là
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 để hiểu danh sách và
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2 thứ hai là toán tử
l = [0, 1, 2]
i = 0

if i in l:
    print(f'{i} is a member of {l}.')
else:
    print(f'{i} is not a member of {l}.')
# 0 is a member of [0, 1, 2].
2

Tại sao là từ điển 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ển có chậm trong Python không?

Python chậm. Tôi cá là bạn có thể gặp phản biện này nhiều lần về việc sử dụng Python, đặc biệt là từ những người đến từ thế giới C hoặc C ++ hoặc Java. Điều này đúng trong nhiều trường hợp, chẳng hạn, việc lặp lại hoặc sắp xếp các mảng, danh sách hoặc từ điển trong Python đôi khi có thể bị chậm

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.

Tuple hay từ điển nào nhanh hơn?

Ai cũng biết rằng trong Python bộ dữ liệu nhanh hơn danh sách và ký tự nhanh hơn đối tượng.