Lọc chuỗi trong Python

Tóm lược. trong hướng dẫn này, bạn sẽ học cách lọc các phần tử danh sách bằng cách sử dụng hàm

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
5 tích hợp sẵn trong Python

Giới thiệu về hàm filter() trong Python

Đôi khi, bạn cần lặp lại các thành phần của danh sách và chọn một số trong số chúng dựa trên các tiêu chí đã chỉ định

Giả sử rằng bạn có danh sách sau đây của

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
6

scores = [70, 60, 80, 90, 50]

Code language: Python (python)

Để lấy tất cả các phần tử từ danh sách

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
6 trong đó mỗi phần tử lớn hơn hoặc bằng 70, bạn sử dụng đoạn mã sau

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)

Làm thế nào nó hoạt động

  • Đầu tiên, xác định một danh sách trống (

    scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

    Code language: Python (python)
    1) sẽ chứa các phần tử từ danh sách

    scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

    Code language: Python (python)
    6
  • Thứ hai, lặp lại các phần tử của danh sách

    scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

    Code language: Python (python)
    6. Nếu phần tử lớn hơn hoặc bằng 70, hãy thêm nó vào danh sách

    scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

    Code language: Python (python)
    1
  • Thứ ba, hiển thị danh sách

    scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

    Code language: Python (python)
    1 lên màn hình

Python có một chức năng tích hợp có tên là

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
5 cho phép bạn lọc một danh sách (hoặc một bộ) theo cách đẹp hơn

Sau đây là cú pháp của hàm

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
5

filter(fn, list)

Code language: Python (python)

Hàm

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
5 lặp qua các phần tử của

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
9 và áp dụng hàm

filter(fn, list)

Code language: Python (python)
0 cho từng phần tử. Nó trả về một iterator cho các phần tử mà

filter(fn, list)

Code language: Python (python)
0 trả về

filter(fn, list)

Code language: Python (python)
2

Trên thực tế, bạn có thể chuyển bất kỳ lần lặp nào tới đối số thứ hai của hàm

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
5, không chỉ một danh sách

Phần sau đây cho biết cách sử dụng hàm

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
5 để trả về danh sách

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
6 trong đó mỗi điểm lớn hơn hoặc bằng 70

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
0

đầu ra

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
1

Vì hàm

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
5 trả về một trình vòng lặp, nên bạn có thể sử dụng vòng lặp

filter(fn, list)

Code language: Python (python)
7 để lặp lại nó. Hoặc bạn có thể sử dụng hàm

filter(fn, list)

Code language: Python (python)
8 để chuyển iterator thành list

Ví dụ sử dụng hàm filter() của Python để lọc danh sách các bộ dữ liệu

Giả sử bạn có danh sách các bộ dữ liệu sau

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
4

Mỗi phần tử trong danh sách là một bộ chứa tên và dân số của quốc gia

Để lấy tất cả các quốc gia có dân số lớn hơn 300 triệu người, bạn có thể sử dụng hàm

scores = [70, 60, 80, 90, 50] filtered = [] for score in scores: if score >= 70: filtered.append(score) print(filtered)

Code language: Python (python)
5 như sau

Bạn có thể sử dụng bộ lọc trên một chuỗi không?

Bạn không thể sử dụng filter() trên một chuỗi vì nó là một Mảng .

Từ khóa bộ lọc trong Python là gì?

Hàm bộ lọc Python() . returns an iterator were the items are filtered through a function to test if the item is accepted or not.