Review Làm thế nào để bạn tìm thấy hậu tố trong python?
Mẹo Hướng dẫn Làm thế nào để bạn tìm thấy hậu tố trong python? 2022
Bùi Đàm Mai Phương đang tìm kiếm từ khóa Làm thế nào để bạn tìm thấy hậu tố trong python? được Update vào lúc : 2022-12-24 20:02:05 . Với phương châm chia sẻ Bí quyết về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi tham khảo Post vẫn ko hiểu thì hoàn toàn có thể lại phản hồi ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.Mẹo Python (>=3. 9)
Nội dung chính Show- Phần kết luậnHậu tố trong Python là gì?Làm cách nào để kiểm tra xem một chuỗi liệu có phải là hậu tố của một chuỗi Python khác không?hậu tố cho ra lệnh là gì?
Bạn hoàn toàn có thể xóa hậu tố của chuỗi bằng
length = 10000 constant_string suffix_matrix 27.2 ms ± 268 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 26.6 ms ± 513 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 22.5 ms ± 36.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 25.1 ms ± 80.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 30.8 ms ± 518 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 36.7 ms ± 1.17 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 33.6 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 39.2 ms ± 2.57 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) random_string suffix_matrix 79.7 ms ± 8.92 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 80.4 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 73.5 ms ± 1.77 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 87.4 ms ± 4.13 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 56.7 ms ± 1.46 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 65.8 ms ± 482 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 57.6 ms ± 970 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 69.7 ms ± 5.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) length = 100000 constant_string suffix_matrix 350 ms ± 23.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 361 ms ± 57.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 288 ms ± 3.15 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 321 ms ± 8.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 398 ms ± 3.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 446 ms ± 9.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 403 ms ± 8.03 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 417 ms ± 1.66 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) random_string suffix_matrix 1.4 s ± 114 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.54 s ± 61.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.43 s ± 130 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.47 s ± 60.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 1.15 s ± 30 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.19 s ± 11.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.15 s ± 40.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.25 s ± 26.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 4Ví dụ: để xóa loại tệp khỏi tên tệp
import pathlib filename = "cv.pdf" file_type_suffix = pathlib.Path(filename).suffix print(filename.removesuffix(file_type_suffix)) # => cv length = 10000 constant_string suffix_matrix 27.2 ms ± 268 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 26.6 ms ± 513 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 22.5 ms ± 36.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 25.1 ms ± 80.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 30.8 ms ± 518 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 36.7 ms ± 1.17 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 33.6 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 39.2 ms ± 2.57 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) random_string suffix_matrix 79.7 ms ± 8.92 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 80.4 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 73.5 ms ± 1.77 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 87.4 ms ± 4.13 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 56.7 ms ± 1.46 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 65.8 ms ± 482 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 57.6 ms ± 970 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 69.7 ms ± 5.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) length = 100000 constant_string suffix_matrix 350 ms ± 23.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 361 ms ± 57.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 288 ms ± 3.15 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 321 ms ± 8.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 398 ms ± 3.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 446 ms ± 9.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 403 ms ± 8.03 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 417 ms ± 1.66 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) random_string suffix_matrix 1.4 s ± 114 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.54 s ± 61.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.43 s ± 130 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.47 s ± 60.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 1.15 s ± 30 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.19 s ± 11.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.15 s ± 40.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.25 s ± 26.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)Chuỗi là một tập hợp tuần tự những ký tự. Khi thao tác với Chuỗi, đôi khi, tất cả chúng ta hoàn toàn có thể cần kiểm tra xem chuỗi có kết thúc bằng một từ rõ ràng hay nói cách khác là kiểm tra xem chuỗi có hậu tố a rõ ràng hay là không
Để kiểm tra xem một chuỗi có kết thúc bằng một chuỗi con hoặc hậu tố đã cho hay là không, tất cả chúng ta hoàn toàn có thể sử dụng phương thức endwith() của lớp chuỗi
Trong hướng dẫn này, tất cả chúng ta sẽ tìm hiểu cách kiểm tra xem một chuỗi có kết thúc bằng một chuỗi con hoặc hậu tố rõ ràng hay là không
cú pháp
Cú pháp của phương thức endwith() là
string.endswith(suffix[, start[, end]])trong đó
length = 10000 constant_string suffix_matrix 27.2 ms ± 268 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 26.6 ms ± 513 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 22.5 ms ± 36.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 25.1 ms ± 80.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 30.8 ms ± 518 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 36.7 ms ± 1.17 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 33.6 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 39.2 ms ± 2.57 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) random_string suffix_matrix 79.7 ms ± 8.92 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 80.4 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 73.5 ms ± 1.77 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 87.4 ms ± 4.13 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 56.7 ms ± 1.46 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 65.8 ms ± 482 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 57.6 ms ± 970 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 69.7 ms ± 5.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) length = 100000 constant_string suffix_matrix 350 ms ± 23.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 361 ms ± 57.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 288 ms ± 3.15 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 321 ms ± 8.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 398 ms ± 3.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 446 ms ± 9.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 403 ms ± 8.03 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 417 ms ± 1.66 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) random_string suffix_matrix 1.4 s ± 114 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.54 s ± 61.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.43 s ± 130 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.47 s ± 60.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 1.15 s ± 30 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.19 s ± 11.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.15 s ± 40.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.25 s ± 26.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 5 là chuỗi con mà chúng tôi đang tìm cách khớp trong chuỗi chính. Đối số length = 10000 constant_string suffix_matrix 27.2 ms ± 268 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 26.6 ms ± 513 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 22.5 ms ± 36.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 25.1 ms ± 80.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 30.8 ms ± 518 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 36.7 ms ± 1.17 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 33.6 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 39.2 ms ± 2.57 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) random_string suffix_matrix 79.7 ms ± 8.92 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 80.4 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 73.5 ms ± 1.77 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 87.4 ms ± 4.13 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 56.7 ms ± 1.46 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 65.8 ms ± 482 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 57.6 ms ± 970 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 69.7 ms ± 5.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) length = 100000 constant_string suffix_matrix 350 ms ± 23.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 361 ms ± 57.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 288 ms ± 3.15 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 321 ms ± 8.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 398 ms ± 3.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 446 ms ± 9.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 403 ms ± 8.03 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 417 ms ± 1.66 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) random_string suffix_matrix 1.4 s ± 114 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.54 s ± 61.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.43 s ± 130 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.47 s ± 60.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 1.15 s ± 30 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.19 s ± 11.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.15 s ± 40.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.25 s ± 26.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 6 và #the string website="https://www.tutorialkart.com" #suffix suffix = 'com' #check if string ends with suffix isValid = website.endswith(suffix) print(isValid)0 là tùy chọn. Chỉ hoàn toàn có thể đề cập đến #the string website="https://www.tutorialkart.com" #suffix suffix = 'com' #check if string ends with suffix isValid = website.endswith(suffix) print(isValid)0 nếu đáp ứng length = 10000 constant_string suffix_matrix 27.2 ms ± 268 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 26.6 ms ± 513 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 22.5 ms ± 36.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 25.1 ms ± 80.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 30.8 ms ± 518 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 36.7 ms ± 1.17 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 33.6 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 39.2 ms ± 2.57 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) random_string suffix_matrix 79.7 ms ± 8.92 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 80.4 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 73.5 ms ± 1.77 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 87.4 ms ± 4.13 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 56.7 ms ± 1.46 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 65.8 ms ± 482 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 57.6 ms ± 970 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 69.7 ms ± 5.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) length = 100000 constant_string suffix_matrix 350 ms ± 23.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 361 ms ± 57.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 288 ms ± 3.15 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 321 ms ± 8.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 398 ms ± 3.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 446 ms ± 9.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 403 ms ± 8.03 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 417 ms ± 1.66 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) random_string suffix_matrix 1.4 s ± 114 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.54 s ± 61.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.43 s ± 130 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.47 s ± 60.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 1.15 s ± 30 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.19 s ± 11.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.15 s ± 40.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.25 s ± 26.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 6Nếu cho trước
length = 10000 constant_string suffix_matrix 27.2 ms ± 268 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 26.6 ms ± 513 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 22.5 ms ± 36.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 25.1 ms ± 80.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 30.8 ms ± 518 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 36.7 ms ± 1.17 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 33.6 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 39.2 ms ± 2.57 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) random_string suffix_matrix 79.7 ms ± 8.92 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 80.4 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 73.5 ms ± 1.77 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 87.4 ms ± 4.13 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 56.7 ms ± 1.46 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 65.8 ms ± 482 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 57.6 ms ± 970 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 69.7 ms ± 5.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) length = 100000 constant_string suffix_matrix 350 ms ± 23.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 361 ms ± 57.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 288 ms ± 3.15 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 321 ms ± 8.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 398 ms ± 3.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 446 ms ± 9.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 403 ms ± 8.03 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 417 ms ± 1.66 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) random_string suffix_matrix 1.4 s ± 114 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.54 s ± 61.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.43 s ± 130 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.47 s ± 60.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 1.15 s ± 30 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.19 s ± 11.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.15 s ± 40.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.25 s ± 26.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 6, thì chuỗi chính từ vị trí đó được xem là khớp với hậu tốNếu đáp ứng
#the string website="https://www.tutorialkart.com" #suffix suffix = 'com' #check if string ends with suffix isValid = website.endswith(suffix) print(isValid)0, chuỗi chính cho tới vị trí đó được xem xét khớp với hậu tốendwith() trả về True nếu chuỗi kết thúc bằng hậu tố, nếu không thì endwith() trả về False
Ghi chú. Bạn cũng hoàn toàn có thể đáp ứng nhiều chuỗi dưới dạng một bộ cho hậu tố. Trong trường hợp đó, kết thúc() trả về true nếu chuỗi kết thúc bằng một trong những chuỗi ở hậu tố. Chúng ta sẽ xem xét ngữ cảnh này với một ví dụ
ví dụ
Kiểm tra xem chuỗi có kết thúc bằng chuỗi hậu tố rõ ràng khôngTrong ví dụ này, chúng tôi đã lấy một url trang web làm chuỗi chính. Chúng tôi sẽ xác thực chuỗi, nếu chuỗi kết thúc bằng hậu tố
#the string website="https://www.tutorialkart.com" #suffix suffix = 'com' #check if string ends with suffix isValid = website.endswith(suffix) print(isValid)5. Để xác thực điều này, tất cả chúng ta sẽ sử dụng chuỗi. phương thức endwith() như được mô tả trong cú phápVí dụ. py
#the string website="https://www.tutorialkart.com" #suffix suffix = 'com' #check if string ends with suffix isValid = website.endswith(suffix) print(isValid)Dùng thử trực tuyếnđầu ra
________số 8Hãy để chúng tôi kiểm tra một số trong những ngữ cảnh tiêu cực
Ví dụ. py
#the string website="https://www.tutorialkart.com" #check if string ends with suffix print(website.endswith('')) print(website.endswith('www')) print(website.endswith('com'))Thử trực tuyếnđầu ra
length = 10000 constant_string suffix_matrix 27.2 ms ± 268 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 26.6 ms ± 513 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 22.5 ms ± 36.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 25.1 ms ± 80.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 30.8 ms ± 518 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 36.7 ms ± 1.17 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 33.6 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 39.2 ms ± 2.57 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) random_string suffix_matrix 79.7 ms ± 8.92 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 80.4 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 73.5 ms ± 1.77 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 87.4 ms ± 4.13 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 56.7 ms ± 1.46 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 65.8 ms ± 482 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 57.6 ms ± 970 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 69.7 ms ± 5.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) length = 100000 constant_string suffix_matrix 350 ms ± 23.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 361 ms ± 57.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 288 ms ± 3.15 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 321 ms ± 8.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 398 ms ± 3.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 446 ms ± 9.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 403 ms ± 8.03 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 417 ms ± 1.66 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) random_string suffix_matrix 1.4 s ± 114 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.54 s ± 61.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.43 s ± 130 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.47 s ± 60.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 1.15 s ± 30 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.19 s ± 11.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.15 s ± 40.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.25 s ± 26.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 0Kiểm tra xem Chuỗi có kết thúc với bất kỳ Hậu tố nào khôngChúng tôi đã lưu ý trong phần cú pháp rằng bạn hoàn toàn có thể chỉ định nhiều chuỗi cho hậu tố
Trong ví dụ này, tất cả chúng ta sẽ lấy nhiều chuỗi làm bộ cho hậu tố và sử dụng phương thức endwith()
Ví dụ. py
length = 10000 constant_string suffix_matrix 27.2 ms ± 268 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 26.6 ms ± 513 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 22.5 ms ± 36.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 25.1 ms ± 80.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 30.8 ms ± 518 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 36.7 ms ± 1.17 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 33.6 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 39.2 ms ± 2.57 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) random_string suffix_matrix 79.7 ms ± 8.92 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 80.4 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 73.5 ms ± 1.77 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 87.4 ms ± 4.13 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 56.7 ms ± 1.46 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 65.8 ms ± 482 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 57.6 ms ± 970 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 69.7 ms ± 5.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) length = 100000 constant_string suffix_matrix 350 ms ± 23.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 361 ms ± 57.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 288 ms ± 3.15 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 321 ms ± 8.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 398 ms ± 3.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 446 ms ± 9.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 403 ms ± 8.03 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 417 ms ± 1.66 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) random_string suffix_matrix 1.4 s ± 114 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.54 s ± 61.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.43 s ± 130 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.47 s ± 60.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 1.15 s ± 30 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.19 s ± 11.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.15 s ± 40.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.25 s ± 26.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1Dùng thử trực tuyếnđầu ra
________số 8Chuỗi của chúng tôi kết thúc bằng một trong những chuỗi được chỉ định trong hậu tố
Kiểm tra xem Chuỗi có kết thúc bằng Hậu tố hay là không khi xem xét [bắt đầu, kết thúc]Trong ví dụ này, tất cả chúng ta sẽ sử dụng những tham số start và end được đề cập trong cú pháp trên
Ví dụ. py
length = 10000 constant_string suffix_matrix 27.2 ms ± 268 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 26.6 ms ± 513 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 22.5 ms ± 36.7 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 25.1 ms ± 80.5 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 30.8 ms ± 518 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 36.7 ms ± 1.17 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 33.6 ms ± 1.49 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 39.2 ms ± 2.57 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) random_string suffix_matrix 79.7 ms ± 8.92 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 80.4 ms ± 1.19 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 73.5 ms ± 1.77 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 87.4 ms ± 4.13 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) suffix_matrix_int 56.7 ms ± 1.46 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) 65.8 ms ± 482 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 57.6 ms ± 970 µs per loop (mean ± std. dev. of 7 runs, 10 loops each) 69.7 ms ± 5.21 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) length = 100000 constant_string suffix_matrix 350 ms ± 23.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 361 ms ± 57.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 288 ms ± 3.15 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 321 ms ± 8.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 398 ms ± 3.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 446 ms ± 9.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 403 ms ± 8.03 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 417 ms ± 1.66 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) random_string suffix_matrix 1.4 s ± 114 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.54 s ± 61.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.43 s ± 130 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.47 s ± 60.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) suffix_matrix_int 1.15 s ± 30 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.19 s ± 11.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.15 s ± 40.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.25 s ± 26.7 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 3Dùng thử trực tuyếnPhần kết luận
Trong Hướng dẫn Python này, tất cả chúng ta đã học cách sử dụng chuỗi. phương thức endwith() để kiểm tra xem một chuỗi có kết thúc bằng tiền tố hay là không bằng những chương trình Python
Post a Comment