Hướng dẫn empty space in list python - không gian trống trong danh sách python

Cách đầu tiên là sử dụng phương thức thay thế () với vòng lặp để xóa không gian trong danh sách Python Một cách tốt hơn để đạt được nó là sử dụng khả năng hiểu danh sách.

Mã ví dụ đơn giản:

Thay thế phương thức + nối với vòng lặp cho vòng lặp

Nó sẽ loại bỏ không gian không phải chuỗi trống.

list1 = ['A', '   ', ' ', 'B', '            ', 'C']

res = []
for ele in list1:
    j = ele.replace(' ', '')
    res.append(j)

print(res)

Output::

Hướng dẫn empty space in list python - không gian trống trong danh sách python

Một cách khác bằng cách sử dụng Loop + Strip ()

Nó sẽ loại bỏ các mục trống khỏi danh sách.

list1 = ['A', '   ', ' ', 'B', '            ', 'C']

res = []
for ele in list1:
    if ele.strip():
        res.append(ele)

print(res)

Output::

Hướng dẫn empty space in list python - không gian trống trong danh sách python

Danh sách cách tốt nhất Hiểu + Dải ()

Cách tiếp cận một lần để thực hiện nhiệm vụ này thay vì sử dụng một vòng lặp.

list1 = ['A', '   ', ' ', 'B', '            ', 'C']

remove_space = [x.strip(' ') for x in list1]
print(remove_space)

delete_empty = [ele for ele in list1 if ele.strip()]
print(delete_empty)

Output::

[’A, một cách khác, ,,,
[‘A’, ‘B’, ‘C’]

Hãy bình luận nếu bạn có bất kỳ nghi ngờ và đề xuất nào về mã danh sách Python này.

Lưu ý: IDE: & NBSP; Pycharm & NBSP; 2021.3.3 (Phiên bản cộng đồng) IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

Tất cả & nbsp; ví dụ python & nbsp; là trong & nbsp; Python & nbsp; 3, vì vậy có thể khác với các phiên bản Python 2 hoặc nâng cấp. Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Hướng dẫn empty space in list python - không gian trống trong danh sách python

Bằng cấp về Khoa học máy tính và Kỹ sư: Nhà phát triển ứng dụng và có nhiều ngôn ngữ lập trình kinh nghiệm. Sự nhiệt tình cho công nghệ và thích học kỹ thuật.

Nhấp vào nhà, sau đó nhấp vào Đoạn> Khoảng cách dòng ..

Làm thế nào để bạn tước một danh sách trong Python?

Trong Python, sử dụng các phương thức danh sách rõ ràng (), pop () và xóa () để xóa các mục (phần tử) khỏi danh sách. Cũng có thể xóa các mục bằng cách sử dụng câu lệnh DEL bằng cách chỉ định một vị trí hoặc phạm vi có chỉ mục hoặc lát cắt.

  • Làm cách nào để xóa một giá trị khỏi danh sách trong Python?
  • Cách xóa một phần tử khỏi danh sách bằng phương thức Remove () trong Python. Để xóa một phần tử khỏi danh sách bằng phương thức Remove (), chỉ định giá trị của phần tử đó và chuyển nó dưới dạng đối số cho phương thức. Xóa () sẽ tìm kiếm danh sách để tìm và xóa nó.
  • Nhấp vào nhà, sau đó nhấp vào Đoạn> Khoảng cách dòng ..

    Làm thế nào để bạn tước một danh sách trong Python?

    Trong Python, sử dụng các phương thức danh sách rõ ràng (), pop () và xóa () để xóa các mục (phần tử) khỏi danh sách. Cũng có thể xóa các mục bằng cách sử dụng câu lệnh DEL bằng cách chỉ định một vị trí hoặc phạm vi có chỉ mục hoặc lát cắt.

    Làm cách nào để xóa một giá trị khỏi danh sách trong Python?Method #1 : Using loop + strip() This is a way in which we can perform this task. In this, we strip the strings using strip(), it reduces to single space and then it can be tested for NULL value. Returns True if string is single space and hence helps in filtering. 

    Python3

    test_list =

    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    1
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    2
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    3
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    4
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    5
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    6

    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    7= test_list 8
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    0
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    1
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    2 test_list
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    5 =4

    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    1
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    22
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    1
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    4
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    5
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    4

    Đầu ra: & nbsp;

    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    7
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    8

    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    1
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    22
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    1
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    4
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    5
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    4

    Đầu ra: & nbsp;

    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']

    & nbsp; Phương pháp số 2: Sử dụng danh sách hiểu + dải () Sự kết hợp của các hàm trên cũng có thể được sử dụng để thực hiện nhiệm vụ này. Trong đó, chúng tôi sử dụng cách tiếp cận một lớp để thực hiện nhiệm vụ này thay vì sử dụng Loop. & NBSP;Method #2 : Using list comprehension + strip() The combination of above functions can also be used to perform this task. In this, we employ one-liner approach to perform this task instead of using loop. 

    Python3

    test_list =

    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    1
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    2
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    3
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    4
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    5
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    6

    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    7= test_list 8
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    0
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    1
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    2 test_list
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    5 =4

    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    1
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    22
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    1
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    4
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    5
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    4

    Đầu ra: & nbsp;

    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']

    Phương pháp số 3: Sử dụng Find ()

    Python3

    test_list =

    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    1
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    2
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    3
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    4
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    5
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    6

    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    7= test_list 8
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    0
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    1
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    2 test_list
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    5 =4

    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    1
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    22
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    1
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    4
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    5
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    4

    Đầu ra: & nbsp;

    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    7
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    8

    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    1
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    22
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    1
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    4
    list1 = ['A', '   ', ' ', 'B', '            ', 'C']
    
    remove_space = [x.strip(' ') for x in list1]
    print(remove_space)
    
    delete_empty = [ele for ele in list1 if ele.strip()]
    print(delete_empty)
    
    5
    The original list is : ['gfg', '   ', ' ', 'is', '            ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']
    4

    Đầu ra

    The original list is : ['gfg', ' ', ' ', 'is', '\t\t ', 'best']
    List after filtering non-empty strings : ['gfg', 'is', 'best']

    Độ phức tạp về thời gian và không gian cho tất cả các phương pháp là như nhau:

    Độ phức tạp về thời gian: O (n)O(n)

    Độ phức tạp không gian: O (n)O(n)


    Làm thế nào để bạn hiển thị không gian trong một danh sách trong Python?

    Phương thức Python ISSpace () được sử dụng để kiểm tra không gian trong chuỗi. Nó returna true nếu chỉ có các ký tự khoảng trắng trong chuỗi. Nếu không thì nó trả về sai. Không gian, Newline và tab, vv được gọi là ký tự khoảng trắng và được xác định trong cơ sở dữ liệu ký tự Unicode là khác hoặc dấu phân cách. is used to check space in the string. It returna true if there are only whitespace characters in the string. Otherwise it returns false. Space, newline, and tabs etc are known as whitespace characters and are defined in the Unicode character database as Other or Separator.

    Làm thế nào để bạn thêm một không gian trong một danh sách?

    Nếu bạn muốn tăng hoặc giảm lượng không gian giữa các mục trong danh sách viên đạn hoặc được đánh số, hãy làm điều này:..
    Chọn toàn bộ danh sách viên đạn ..
    Nhấp vào nhà, sau đó nhấp vào Đoạn> Khoảng cách dòng ..

    Làm thế nào để bạn tước một danh sách trong Python?

    Trong Python, sử dụng các phương thức danh sách rõ ràng (), pop () và xóa () để xóa các mục (phần tử) khỏi danh sách.Cũng có thể xóa các mục bằng cách sử dụng câu lệnh DEL bằng cách chỉ định một vị trí hoặc phạm vi có chỉ mục hoặc lát cắt.use list methods clear() , pop() , and remove() to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.

    Làm cách nào để xóa một giá trị khỏi danh sách trong Python?

    Cách xóa một phần tử khỏi danh sách bằng phương thức Remove () trong Python.Để xóa một phần tử khỏi danh sách bằng phương thức Remove (), chỉ định giá trị của phần tử đó và chuyển nó dưới dạng đối số cho phương thức.Xóa () sẽ tìm kiếm danh sách để tìm và xóa nó.specify the value of that element and pass it as an argument to the method. remove() will search the list to find it and remove it.