Hướng dẫn python remove blank lines from list - python xóa các dòng trống khỏi danh sách

Theo báo cáo của Aziz Alto filter[None, lstr] không loại bỏ các chuỗi trống bằng không gian

>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
0 nhưng nếu bạn chắc chắn LSTR chỉ chứa chuỗi, bạn có thể sử dụng
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
1

>>> lstr = ['hello', '', ' ', 'world', ' ']
>>> lstr
['hello', '', ' ', 'world', ' ']
>>> ' '.join[lstr].split[]
['hello', 'world']
>>> filter[str.strip, lstr]
['hello', 'world']

So sánh thời gian trên PC của tôi

>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825

Giải pháp nhanh nhất để loại bỏ

>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
2 và các chuỗi trống với không gian
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
0 vẫn còn
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
4.

Như đã báo cáo trong một bình luận, tình huống là khác nhau nếu chuỗi của bạn chứa không gian.

>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']

Bạn có thể thấy rằng

>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
1 bảo tồn các chuỗi có không gian trên đó nhưng
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
4 sẽ phân chia chuỗi này.

Trong nhiều tình huống, chúng tôi gặp phải vấn đề nhận được một chuỗi trống trong một lượng lớn dữ liệu và xử lý đôi khi trở thành một nhiệm vụ tẻ nhạt. Hãy để thảo luận về một số cách nhất định để loại bỏ các chuỗi trống khỏi danh sách các chuỗi. & NBSP;

Phương pháp số 1: Sử dụng Remove [] Phương thức cụ thể này khá ngây thơ và không được khuyến nghị sử dụng, nhưng thực sự là một phương pháp để thực hiện nhiệm vụ này. loại bỏ [] thường loại bỏ sự xuất hiện đầu tiên của một chuỗi trống và chúng tôi giữ cho quá trình này cho đến khi không tìm thấy chuỗi trống nào trong danh sách. & nbsp; This particular method is quite naive and not recommended use, but is indeed a method to perform this task. remove[] generally removes the first occurrence of an empty string and we keep iterating this process until no empty string is found in list. 

Python3

>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
7
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
8
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
9
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
1
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
4
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
6
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
8

>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
9
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
0
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
1
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
2
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
3
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
4

Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
5
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
6
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
7
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
8

Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
9
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
0

>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
9
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
0
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
3
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
2
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
3
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
4

Output:

Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']

Phương pháp số 2: Sử dụng danh sách Hiểu rõ hơn và cách tiếp cận tốt hơn để xóa tất cả các chuỗi trống, nó chỉ kiểm tra xem chuỗi không trống và tạo lại danh sách với tất cả các chuỗi không trống. & NBSP; More concise and better approach to remove all the empty strings, it just checks if the string is not empty and re-makes the list with all strings that are not empty. 

Python3

>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
7
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
8
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
9
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
1
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
4
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
6
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
8

>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
9
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
0
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
1
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
2
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
3
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
4

Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
5
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
6
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
7
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
8

>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
9
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
0
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
3
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
2
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
3
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
4

Output:

Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']

Phương pháp số 2: Sử dụng danh sách Hiểu rõ hơn và cách tiếp cận tốt hơn để xóa tất cả các chuỗi trống, nó chỉ kiểm tra xem chuỗi không trống và tạo lại danh sách với tất cả các chuỗi không trống. & NBSP;Method #3 : Using join[] + split[] Combining both the join[] and split[] operations, this task can also be achieved. We first join all the strings so that empty space is removed, and then split it back to list so that the new list made now has no empty string. 

Python3

>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
7
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
8
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
9
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
1
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
4
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
6
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
8

>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
9
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
0
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
1
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
2
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
3
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
4

Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
5
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
6
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
7
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
8

>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
9
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
0
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
3
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
2
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
3
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
4

Output:

Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']

Phương pháp số 2: Sử dụng danh sách Hiểu rõ hơn và cách tiếp cận tốt hơn để xóa tất cả các chuỗi trống, nó chỉ kiểm tra xem chuỗi không trống và tạo lại danh sách với tất cả các chuỗi không trống. & NBSP; Using filter[] is the most elegant and fastest way to perform this task. This method is highly recommended because speed matters when we deal with large machine learning data set that may potentially contain empty string. 

Python3

>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
7
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
8
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
9
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
1
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
4
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
6
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
8

>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
9
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
0
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
1
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
2
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
3
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
4

Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
5
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
6
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
7
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
8

>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
9
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
0
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
3
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
2
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
3
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
4

Output:

Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']

Phương pháp số 2: Sử dụng danh sách Hiểu rõ hơn và cách tiếp cận tốt hơn để xóa tất cả các chuỗi trống, nó chỉ kiểm tra xem chuỗi không trống và tạo lại danh sách với tất cả các chuỗi không trống. & NBSP; Using len[] method.

Python3

>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
7
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
8
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
9
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
1
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
4
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
6
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
0
>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
8

>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
7
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
8
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
7
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
8
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
9
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
7
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
7__

& nbsp; Phương thức số 3: Sử dụng tham gia [] + split [] Kết hợp cả hai thao tác tham gia [] và split [], tác vụ này cũng có thể đạt được. Trước tiên chúng tôi tham gia tất cả các chuỗi để loại bỏ không gian trống, sau đó chia lại danh sách để danh sách mới được thực hiện hiện không có chuỗi trống. & NBSP;

Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
9
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
2
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
0
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
65
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
66
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
8
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
8
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
69
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
70

>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
71
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
72

>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
7
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
8 ____1010____101

Phương thức số 4: Sử dụng Filter [] sử dụng Filter [] là cách thanh lịch và nhanh nhất để thực hiện tác vụ này. Phương pháp này rất được khuyến khích bởi vì tốc độ quan trọng khi chúng tôi xử lý tập dữ liệu học máy lớn có khả năng chứa chuỗi trống. & NBSP;

Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']

>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
7
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
8
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
28
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
0
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
30
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
0____132
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
33

Python3

Phương pháp số 5: Sử dụng phương thức Len [].

>>> lstr = ['hello', '', ' ', 'world', '    ', 'see you']
>>> lstr
['hello', '', ' ', 'world', '    ', 'see you']
>>> ' '.join[lstr].split[]
['hello', 'world', 'see', 'you']
>>> filter[str.strip, lstr]
['hello', 'world', 'see you']
9
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
0
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
1
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
2
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
3
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
4

Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
8
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
9
Original list is : ['', 'GeeksforGeeks', '', 'is', 'best', '']
Modified list is : ['GeeksforGeeks', 'is', 'best']
7
>>> from timeit import timeit
>>> timeit['" ".join[lstr].split[]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
3.356455087661743
>>> timeit['filter[str.strip, lstr]', "lstr=['hello', '', ' ', 'world', ' ']", number=10000000]
5.276503801345825
61

Phương thức số 4: Sử dụng Filter [] sử dụng Filter [] là cách thanh lịch và nhanh nhất để thực hiện tác vụ này. Phương pháp này rất được khuyến khích bởi vì tốc độ quan trọng khi chúng tôi xử lý tập dữ liệu học máy lớn có khả năng chứa chuỗi trống. & NBSP;

['GeeksforGeeks', 'is', 'best']


Làm cách nào để loại bỏ các dòng trống khỏi danh sách trong Python?

Phương pháp số 1: Sử dụng Remove [] Phương thức cụ thể này khá ngây thơ và không được khuyến nghị sử dụng, nhưng thực sự là một phương pháp để thực hiện nhiệm vụ này.Xóa [] thường loại bỏ sự xuất hiện đầu tiên của một chuỗi trống và chúng tôi giữ cho quá trình này cho đến khi không tìm thấy chuỗi trống nào trong danh sách.Using remove[] This particular method is quite naive and not recommended use, but is indeed a method to perform this task. remove[] generally removes the first occurrence of an empty string and we keep iterating this process until no empty string is found in list.

Làm cách nào để loại bỏ các hàng trống khỏi danh sách?

Một cách nhanh chóng để xóa hoặc loại bỏ các hàng trống trong Excel..
Nhấn [F5] ..
Trong hộp thoại đi đến kết quả, nhấp vào Đặc biệt ..
Nhấp vào tùy chọn Blanks và nhấp vào OK.....
Bây giờ bạn đã sẵn sàng để xóa các ô đã chọn.....
Excel sẽ xóa các ô trống khỏi phạm vi dữ liệu đã chọn ..

Làm cách nào để loại bỏ các dòng trống khỏi một chuỗi?

^\ S+$ sẽ loại bỏ mọi thứ từ dòng trống đầu tiên đến cuối cùng [trong một khối tiếp giáp của các đường trống], bao gồm các dòng chỉ chứa các tab hoặc khoảng trắng.. will remove everything from the first blank line to the last [in a contiguous block of empty lines], including lines that only contain tabs or spaces. [\r\n]* will then remove the last CRLF [or just LF which is important because the . NET regex engine matches the $ between a \r and a \n , funnily enough].

Bài Viết Liên Quan

Chủ Đề