Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận In this method, we use list comprehension for traversal logic and the endswith method to filter out all the strings that end with a particular letter. The left strings can be used to make a different list.
Python3
Đôi khi, chúng ta cần lọc danh sách với ký tự cuối cùng của mỗi chuỗi trong danh sách. Loại nhiệm vụ này có nhiều ứng dụng trong lập trình hàng ngày và ngay cả trong miền lập trình cạnh tranh. Hãy để thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.
Phương pháp số 1: Sử dụng danh sách hiểu + endswith [] Trong phương thức này, chúng tôi sử dụng danh sách hiểu cho logic truyền tải và phương thức endSwith để lọc tất cả các chuỗi kết thúc bằng một chữ cái cụ thể. Các chuỗi bên trái có thể được sử dụng để tạo một danh sách khác. & NBSP;
test_list
=
[
'apple'
,
'oranges'
____7
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']010 51212
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']4
=
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']6
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']7
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']8
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']9
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']0
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']1
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']2
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']3
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']4
=
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']6
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']7
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']8
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']9
test_list
test_list
1 test_list
2test_list
3=
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']6
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']7
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']8
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']9
test_list
test_list
1The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
7=
6The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
9 =
8The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
1 The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
22[
1
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']
The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
7=
6The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
9 [
5The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
1 The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
22[
8 This task can be performed using the filter function with performs the similar task internally as the above function and lambda is helper function to build the logic.
Python3
Đôi khi, chúng ta cần lọc danh sách với ký tự cuối cùng của mỗi chuỗi trong danh sách. Loại nhiệm vụ này có nhiều ứng dụng trong lập trình hàng ngày và ngay cả trong miền lập trình cạnh tranh. Hãy để thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.
Phương pháp số 1: Sử dụng danh sách hiểu + endswith [] Trong phương thức này, chúng tôi sử dụng danh sách hiểu cho logic truyền tải và phương thức endSwith để lọc tất cả các chuỗi kết thúc bằng một chữ cái cụ thể. Các chuỗi bên trái có thể được sử dụng để tạo một danh sách khác. & NBSP;
test_list
=
[
'apple'
,
'oranges'
____7
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']010 51212
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']4
=
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']6
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']7
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']8
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']9
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']0
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']1
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']2
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']3
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']4
=
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']6
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']7
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']8
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']9
test_list
test_list
1 test_list
2test_list
3=
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']6
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']7
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']8
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']9
test_list
test_list
1The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
7=
6The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
9 =
8The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
1 The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
22[
1
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']
The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
7=
6The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
9 [
5The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
1 The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
22[
8
Python3
Đôi khi, chúng ta cần lọc danh sách với ký tự cuối cùng của mỗi chuỗi trong danh sách. Loại nhiệm vụ này có nhiều ứng dụng trong lập trình hàng ngày và ngay cả trong miền lập trình cạnh tranh. Hãy để thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện.
Phương pháp số 1: Sử dụng danh sách hiểu + endswith [] Trong phương thức này, chúng tôi sử dụng danh sách hiểu cho logic truyền tải và phương thức endSwith để lọc tất cả các chuỗi kết thúc bằng một chữ cái cụ thể. Các chuỗi bên trái có thể được sử dụng để tạo một danh sách khác. & NBSP;
test_list
=
[
'apple'
,
'oranges'
____7
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']010 51212
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']4
=
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']6
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']7
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']8
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']9
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']0
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']1
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']2
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']3
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']4
=
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']6
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']7
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']8
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']9
test_list
test_list
1 test_list
2The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']42
test_list
1The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']44
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']45
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']46
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']3
=
=
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']50
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']51
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']52
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']42
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']54
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']55
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']51
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']57
test_list
3=
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']6
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']7
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']8
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']9
test_list
test_list
1The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']7
=
6The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']9
=
8The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']1
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']22
[
1The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
7=
6The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
9 [
5The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
1 The original list : ['apple', 'oranges', 'mango', 'grapes']
The list without suffix s : ['apple', 'mango']
The list with suffix s : ['oranges', 'grapes']
22[
8
The original list : ['apple', 'oranges', 'mango', 'grapes'] The list without suffix s : ['apple', 'mango'] The list with suffix s : ['oranges', 'grapes']