Khớp trong danh sách python

Tại một số thời điểm trong hành trình Python của bạn, bạn có thể cần tìm mục đầu tiên khớp với một tiêu chí nhất định trong Python iterable, chẳng hạn như danh sách hoặc từ điển

Trường hợp đơn giản nhất là bạn cần xác nhận rằng một mục cụ thể tồn tại trong iterable. Ví dụ: bạn muốn tìm một tên trong danh sách tên hoặc một chuỗi con bên trong một chuỗi. Trong những trường hợp này, tốt nhất bạn nên sử dụng toán tử

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
5. Tuy nhiên, có nhiều trường hợp sử dụng khi bạn có thể muốn tìm các mục có thuộc tính cụ thể. Ví dụ, bạn có thể cần phải

  • Tìm một giá trị khác 0 trong danh sách các số
  • Tìm tên có độ dài cụ thể trong danh sách các chuỗi
  • Tìm và sửa đổi từ điển trong danh sách từ điển dựa trên một thuộc tính nhất định

Hướng dẫn này sẽ đề cập đến cách tốt nhất để tiếp cận cả ba kịch bản. Một tùy chọn là chuyển đổi toàn bộ lần lặp của bạn sang một danh sách mới và sau đó sử dụng

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
6 để tìm mục đầu tiên phù hợp với tiêu chí của bạn

>>>

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'

Ở đây, bạn đã sử dụng

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
6 để thấy rằng
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
8 là tên đầu tiên trong danh sách của bạn với bảy ký tự. Giải pháp này không tuyệt vời, một phần vì bạn tính toán tiêu chí cho tất cả các phần tử, ngay cả khi mục đầu tiên khớp

Trong các tình huống trên, bạn đang tìm kiếm một thuộc tính được tính toán của các mục mà bạn đang lặp lại. Trong hướng dẫn này, bạn sẽ học cách khớp một thuộc tính dẫn xuất như vậy mà không cần thực hiện các phép tính không cần thiết

Mã mẫu. Nhấp vào đây để tải xuống mã nguồn miễn phí mà bạn sẽ sử dụng để tìm kết quả khớp đầu tiên trong danh sách Python hoặc có thể lặp lại

Cách lấy mục phù hợp đầu tiên trong danh sách Python

Có thể bạn đã biết về toán tử

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
5 Python, toán tử này có thể cho bạn biết liệu một mục có trong một lần lặp hay không. Mặc dù đây là phương pháp hiệu quả nhất mà bạn có thể sử dụng cho mục đích này, đôi khi bạn có thể cần so khớp dựa trên thuộc tính được tính toán của các mục, chẳng hạn như độ dài của chúng

Ví dụ: bạn có thể đang làm việc với danh sách từ điển, điển hình là những gì bạn có thể nhận được khi xử lý dữ liệu JSON. Kiểm tra dữ liệu này được lấy từ

>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
0

>>>

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]

Bạn có thể muốn lấy từ điển đầu tiên có dân số hơn một trăm triệu. Toán tử

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
5 không phải là một lựa chọn tuyệt vời vì hai lý do. Một, bạn cần có từ điển đầy đủ để khớp với nó và hai, nó sẽ không trả về đối tượng thực mà là một giá trị Boolean

>>>

>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True

Không có cách nào để sử dụng

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
5 nếu bạn cần tìm từ điển dựa trên một thuộc tính của từ điển, chẳng hạn như dân số

Cách dễ đọc nhất để tìm và thao tác phần tử đầu tiên trong danh sách dựa trên giá trị được tính toán là sử dụng vòng lặp

>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
3 khiêm tốn

>>>

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
2

Thay vì in đối tượng đích, bạn có thể làm bất cứ điều gì bạn thích với nó trong thân vòng lặp

>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
3. Sau khi bạn hoàn thành, hãy nhớ ngắt vòng lặp
>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
3 để bạn không cần phải tìm kiếm phần còn lại của danh sách

Ghi chú. Sử dụng câu lệnh

>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
6 áp dụng nếu bạn đang tìm kiếm kết quả khớp đầu tiên từ lần lặp. Nếu bạn đang muốn lấy hoặc xử lý tất cả các kết quả phù hợp, thì bạn có thể thực hiện mà không cần
>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
6

Cách tiếp cận vòng lặp

>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
3 là cách tiếp cận của gói
>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
9, đây là một gói nhỏ mà bạn có thể tải xuống từ PyPI hiển thị một chức năng có mục đích chung,
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
20. Theo mặc định, hàm này trả về giá trị trung thực đầu tiên từ một lần lặp, với tham số
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
21 tùy chọn để trả về giá trị trung thực đầu tiên sau khi nó được chuyển qua đối số
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
21

Ghi chú. Trên Python 3. 10 trở lên, bạn có thể sử dụng khớp mẫu cấu trúc để khớp các loại cấu trúc dữ liệu này theo cách mà bạn có thể thích. Ví dụ: bạn có thể tìm kiếm quốc gia đầu tiên có dân số hơn một trăm triệu người như sau

>>>

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
2

Tại đây, bạn sử dụng một người bảo vệ để chỉ khớp với một số quần thể nhất định

Sử dụng khớp mẫu cấu trúc thay vì các câu điều kiện thông thường có thể dễ đọc và ngắn gọn hơn nếu các mẫu khớp đủ phức tạp

Ở phần sau của hướng dẫn này, bạn sẽ triển khai biến thể hàm

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
20 của riêng mình. Nhưng trước tiên, bạn sẽ xem xét một cách khác để trả lại kết quả khớp đầu tiên. sử dụng máy phát điện

Loại bỏ các quảng cáo

Sử dụng Trình tạo Python để lấy kết quả khớp đầu tiên

Trình lặp trình tạo Python là các trình lặp tiết kiệm bộ nhớ có thể được sử dụng để tìm phần tử đầu tiên trong danh sách hoặc bất kỳ trình lặp nào. Chúng là một tính năng cốt lõi của Python, được sử dụng rộng rãi dưới mui xe. Có thể bạn đã từng sử dụng máy phát điện mà không hề hay biết

Vấn đề tiềm ẩn với các trình tạo là chúng trừu tượng hơn một chút và do đó, không hoàn toàn dễ đọc như các vòng lặp

>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
3. Bạn nhận được một số lợi ích về hiệu suất từ ​​các trình tạo, nhưng những lợi ích này thường không đáng kể khi xem xét tầm quan trọng của khả năng đọc. Điều đó nói rằng, sử dụng chúng có thể thú vị và thực sự nâng cấp trò chơi Python của bạn

Trong Python, bạn có thể tạo trình tạo theo nhiều cách khác nhau, nhưng trong hướng dẫn này, bạn sẽ làm việc với khả năng hiểu trình tạo

>>>

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
5

Sau khi bạn đã xác định trình lặp trình tạo, bạn có thể gọi hàm

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
25 với trình tạo, tạo từng quốc gia một cho đến khi hết danh sách
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
26

Để tìm phần tử đầu tiên khớp với một tiêu chí nhất định trong danh sách, bạn có thể thêm biểu thức điều kiện vào phần hiểu trình tạo để trình vòng lặp kết quả sẽ chỉ tạo ra các mục khớp với tiêu chí của bạn. Trong ví dụ sau, bạn sử dụng biểu thức điều kiện để tạo các mục dựa trên việc liệu thuộc tính

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
27 của chúng có trên một trăm triệu hay không

>>>

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
9

Vì vậy, bây giờ trình tạo sẽ chỉ tạo từ điển có thuộc tính

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
27 trên một trăm triệu. Điều này có nghĩa là lần đầu tiên bạn gọi
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
25 bằng trình vòng lặp trình tạo, nó sẽ trả về phần tử đầu tiên mà bạn đang tìm kiếm trong danh sách, giống như phiên bản vòng lặp
>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
3

Ghi chú. Bạn sẽ nhận được một ngoại lệ nếu bạn gọi

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
25 và không có kết quả khớp hoặc trình tạo đã cạn kiệt. Để ngăn chặn điều này, bạn có thể chuyển một đối số
>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
22 sang
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
25

>>>

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
6

Khi trình tạo kết thúc việc tạo kết quả khớp, nó sẽ trả về giá trị mặc định được truyền vào. Vì bạn đang quay lại

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
24, nên bạn không nhận được đầu ra nào trên REPL. Nếu bạn chưa chuyển vào giá trị mặc định, bạn sẽ nhận được một ngoại lệ
>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
25

Xét về khả năng đọc, trình tạo không hoàn toàn tự nhiên như vòng lặp

>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
3. Vậy tại sao bạn có thể muốn sử dụng một cái cho mục đích này?

So sánh hiệu suất giữa các vòng lặp và trình tạo

Như thường lệ, khi đo lường hiệu suất, bạn không nên đọc quá nhiều vào bất kỳ một nhóm kết quả nào. Thay vào đó, hãy thiết kế một thử nghiệm cho mã của riêng bạn với dữ liệu trong thế giới thực của riêng bạn trước khi bạn đưa ra bất kỳ quyết định quan trọng nào. Bạn cũng cần cân nhắc giữa độ phức tạp và khả năng đọc—có lẽ bỏ đi vài phần nghìn giây là không đáng

Đối với thử nghiệm này, bạn sẽ muốn tạo một hàm có thể tạo danh sách có kích thước tùy ý với một giá trị nhất định tại một vị trí nhất định

>>>

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
0

Hàm

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
27 tạo danh sách chứa các mục giống hệt nhau. Tất cả các mục trong danh sách, ngoại trừ một mục, là bản sao của đối số
>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
28. Ngoại lệ duy nhất là đối số
>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
29 và nó được đặt ở chỉ mục được cung cấp bởi đối số
>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
50

Bạn đã nhập

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
51 và sử dụng nó để xuất danh sách đã tạo để dễ đọc hơn. Nếu không, danh sách sẽ xuất hiện trên một dòng theo mặc định

Với chức năng này, bạn sẽ có thể tạo một tập hợp lớn các danh sách với giá trị đích ở các vị trí khác nhau trong danh sách. Bạn có thể sử dụng điều này để so sánh mất bao lâu để tìm một phần tử ở đầu và ở cuối danh sách

Để so sánh các vòng lặp và trình tạo

>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
3, bạn sẽ cần thêm hai hàm cơ bản được mã hóa cứng để tìm một từ điển có thuộc tính
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
27 trên năm mươi

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
8

Các chức năng được mã hóa cứng để giữ mọi thứ đơn giản cho bài kiểm tra. Trong phần tiếp theo, bạn sẽ tạo một chức năng có thể tái sử dụng

Với các thành phần cơ bản này, bạn có thể thiết lập tập lệnh với

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
54 để kiểm tra cả hai chức năng khớp với một loạt danh sách có vị trí đích và các vị trí khác nhau trong danh sách

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
0

Tập lệnh này sẽ tạo ra hai danh sách song song, mỗi danh sách chứa thời gian cần thiết để tìm phần tử bằng vòng lặp hoặc trình tạo. Tập lệnh cũng sẽ tạo danh sách thứ ba chứa vị trí tương ứng của phần tử đích trong danh sách

Bạn chưa làm bất cứ điều gì với kết quả và lý tưởng nhất là bạn muốn lập biểu đồ cho những kết quả này. Vì vậy, hãy xem tập lệnh hoàn chỉnh sau sử dụng

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
55 để tạo một vài biểu đồ từ đầu ra

Trận đấu đầu tiên trên biểu đồHiển thị/Ẩn

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
1

Tùy thuộc vào hệ thống bạn đang chạy và các giá trị cho

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
56,
>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
57 và
>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
58 mà bạn sử dụng, việc chạy tập lệnh có thể mất một lúc, nhưng tập lệnh sẽ tạo ra một biểu đồ hiển thị thời gian được vẽ trên biểu đồ của nhau

Ngoài ra, sau khi đóng biểu đồ đầu tiên, bạn sẽ nhận được một biểu đồ khác hiển thị tỷ lệ giữa hai chiến lược

Biểu đồ cuối cùng này minh họa rõ ràng rằng trong thử nghiệm này, khi mục đích ở gần đầu trình vòng lặp, trình tạo chậm hơn nhiều so với vòng lặp

>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
3. Tuy nhiên, một khi phần tử cần tìm ở vị trí 100 hoặc cao hơn, các trình tạo sẽ vượt qua vòng lặp
>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
3 khá ổn định và với tỷ suất lợi nhuận hợp lý

Bạn có thể phóng to biểu đồ trước đó một cách tương tác bằng biểu tượng kính lúp. Biểu đồ được phóng to cho thấy rằng có mức tăng hiệu suất khoảng năm hoặc sáu phần trăm. Năm phần trăm có thể không phải là bất cứ điều gì để viết về nhà, nhưng nó cũng không đáng kể. Việc nó có xứng đáng với bạn hay không tùy thuộc vào dữ liệu cụ thể mà bạn sẽ sử dụng và tần suất bạn cần sử dụng dữ liệu đó

Ghi chú. Đối với các giá trị thấp của

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
56, bạn sẽ thường thấy biểu đồ tăng đột biến, đây là tác dụng phụ không thể tránh khỏi khi thử nghiệm trên máy tính không dành riêng cho thử nghiệm

Nếu máy tính cần làm gì đó, thì nó sẽ tạm dừng quy trình Python mà không do dự và điều này có thể làm tăng một số kết quả nhất định. Nếu bạn lặp lại thử nghiệm nhiều lần, thì các gai sẽ xuất hiện ở các vị trí ngẫu nhiên

Để làm mịn các dòng, hãy tăng giá trị của

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
56

Với những kết quả đó, bạn có thể tạm thời nói rằng trình tạo nhanh hơn vòng lặp

>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
3, mặc dù trình tạo có thể chậm hơn đáng kể khi mục cần tìm nằm trong hàng trăm phần tử đầu tiên của trình lặp. Khi bạn đang xử lý các danh sách nhỏ, sự khác biệt tổng thể về số mili giây thô bị mất là không nhiều. Tuy nhiên, đối với các lần lặp lớn trong đó mức tăng 5 phần trăm có thể có nghĩa là vài phút, đó là điều cần lưu ý

Như bạn có thể thấy trong biểu đồ cuối cùng này, đối với các lần lặp rất lớn, mức tăng hiệu suất ổn định ở mức khoảng 6 phần trăm. Ngoài ra, hãy bỏ qua các đột biến—để kiểm tra khả năng lặp lại lớn này,

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
56 đã giảm đáng kể

Loại bỏ các quảng cáo

Tạo một hàm Python có thể tái sử dụng để tìm kết quả khớp đầu tiên

Giả sử rằng các lần lặp mà bạn muốn sử dụng sẽ có quy mô lớn và bạn quan tâm đến việc loại bỏ mọi bit hiệu suất ra khỏi mã của mình. Vì lý do đó, bạn sẽ sử dụng bộ tạo thay vì vòng lặp

>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
3. Bạn cũng sẽ xử lý nhiều lần lặp khác nhau với nhiều mục khác nhau và muốn có sự linh hoạt trong cách phối hợp, vì vậy, bạn sẽ thiết kế chức năng của mình để có thể đạt được nhiều mục tiêu khác nhau

  • Trả về giá trị trung thực đầu tiên
  • Trả lại trận đấu đầu tiên
  • Trả về kết quả trung thực đầu tiên của các giá trị được chuyển qua một hàm chính
  • Trả về kết quả khớp đầu tiên của các giá trị được chuyển qua một hàm chính
  • Trả về giá trị mặc định nếu không khớp

Mặc dù có nhiều cách để thực hiện điều này, nhưng đây là một cách để thực hiện với khớp mẫu

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
2

Bạn có thể gọi hàm với tối đa bốn đối số và hàm sẽ hoạt động khác nhau tùy thuộc vào sự kết hợp của các đối số mà bạn truyền vào hàm đó

Hành vi của hàm chủ yếu phụ thuộc vào các đối số

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
29 và
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
21. Đó là lý do tại sao câu lệnh
>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
98 kiểm tra xem
>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
99 có sử dụng hàm
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
60 để tìm hiểu xem
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
21 có phải là một hàm hay không

Ví dụ: nếu cả hai điều kiện của

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
98 đều là
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
63, thì điều đó có nghĩa là bạn đã vượt qua
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
21 nhưng không có
>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
29. Điều này có nghĩa là bạn muốn mỗi mục trong iterable được chuyển qua hàm
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
21 và giá trị trả về phải là kết quả trung thực đầu tiên

Một ví dụ khác, nếu cả hai điều kiện của

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
98 đều là
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
68, điều đó có nghĩa là bạn đã chuyển vào một giá trị nhưng không phải là
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
21. Vượt qua một
>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
29 và không có
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
21 có nghĩa là bạn muốn phần tử đầu tiên trong iterable khớp trực tiếp với giá trị được cung cấp

Khi

>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
98 kết thúc, bạn có máy phát điện của mình. Tất cả những gì còn lại phải làm là gọi
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
25 với trình tạo và đối số
>>> names = ["Linda", "Tiffany", "Florina", "Jovann"]
>>> length_of_names = [len[name] for name in names]
>>> idx = length_of_names.index[7]
>>> names[idx]
'Tiffany'
22 cho trận đấu đầu tiên

Với chức năng này, bạn có thể tìm kiếm các trận đấu theo bốn cách khác nhau

>>>

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
3

Với chức năng này, bạn có rất nhiều sự linh hoạt trong cách khớp. Chẳng hạn, bạn có thể chỉ xử lý các giá trị hoặc chỉ các hàm

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
21 hoặc cả hai

Trong gói

>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
9 đã đề cập trước đó, chữ ký hàm hơi khác một chút. Nó không có tham số giá trị. Bạn vẫn có thể đạt được hiệu quả tương tự như trên bằng cách dựa vào tham số
>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
21

>>>

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
4

Trong các tài liệu có thể tải xuống, bạn cũng có thể tìm thấy cách triển khai thay thế của

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
08 phản ánh chữ ký của gói
>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
9

Mã mẫu. Nhấp vào đây để tải xuống mã nguồn miễn phí mà bạn sẽ sử dụng để tìm kết quả khớp đầu tiên trong danh sách Python hoặc có thể lặp lại

Bất kể cuối cùng bạn sử dụng cách triển khai nào, giờ đây bạn đã có một chức năng hiệu quả, có thể tái sử dụng để có thể lấy mục đầu tiên bạn cần

Sự kết luận

Trong hướng dẫn này, bạn đã học cách tìm phần tử đầu tiên trong danh sách hoặc bất kỳ phần tử lặp nào theo nhiều cách khác nhau. Bạn đã biết rằng cách nhanh nhất và cơ bản nhất để so khớp là sử dụng toán tử

>>> countries = [
..     {"country": "Austria", "population": 8_840_521},
..     {"country": "Canada", "population": 37_057_765},
..     {"country": "Cuba", "population": 11_338_138},
..     {"country": "Dominican Republic", "population": 10_627_165},
..     {"country": "Germany", "population": 82_905_782},
..     {"country": "Norway", "population": 5_311_916},
..     {"country": "Philippines", "population": 106_651_922},
..     {"country": "Poland", "population": 37_974_750},
..     {"country": "Scotland", "population": 5_424_800},
..     {"country": "United States", "population": 326_687_501},
.. ]
5, nhưng bạn đã thấy rằng nó bị giới hạn đối với bất kỳ thứ gì phức tạp hơn. Vậy là bạn đã kiểm tra vòng lặp
>>> target_country = {"country": "Philippines", "population": 106_651_922}
>>> target_country in countries
True
3 khiêm tốn, đây sẽ là cách đơn giản và dễ đọc nhất. Tuy nhiên, bạn cũng đã xem xét các trình tạo để có thêm một chút hiệu suất và độ vênh

Cuối cùng, bạn đã xem xét một triển khai có thể có của hàm lấy mục đầu tiên từ một lần lặp, cho dù đó là giá trị trung thực đầu tiên hay giá trị được biến đổi bởi một hàm khớp với các tiêu chí nhất định

Đánh dấu là đã hoàn thành

🐍 Thủ thuật Python 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Gửi cho tôi thủ thuật Python »

Giới thiệu về Ian Currie

Ian là một mọt sách Python, người sử dụng nó cho mọi thứ, từ mày mò đến giúp mọi người và công ty quản lý công việc hàng ngày và phát triển doanh nghiệp của họ

» Thông tin thêm về Ian

Mỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là

Aldren

Geir Arne

kate

Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bạn nghĩ sao?

Đánh giá bài viết này

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. Nhận các mẹo để đặt câu hỏi hay và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi

Chủ Đề