Hướng dẫn is there a remove method in python? - có một phương pháp loại bỏ trong python?

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức Danh sách Python () với sự trợ giúp của các ví dụ.

Phương thức

list.remove(element)
2 loại bỏ phần tử khớp đầu tiên (được truyền dưới dạng đối số) khỏi danh sách.

Thí dụ

# create a list
prime_numbers = [2, 3, 5, 7, 9, 11]

# remove 9 from the list prime_numbers.remove(9)

# Updated prime_numbers List print('Updated List: ', prime_numbers) # Output: Updated List: [2, 3, 5, 7, 11]


Cú pháp của danh sách xóa ()

Cú pháp của phương thức

list.remove(element)
2 là:

list.remove(element)

loại bỏ () tham số

  • Phương thức
    list.remove(element)
    2 lấy một phần tử duy nhất làm đối số và loại bỏ nó khỏi danh sách.
  • Nếu
    list.remove(element)
    5 không tồn tại, nó sẽ ném valueError: list.remove (x): x không trong ngoại lệ danh sách.ValueError: list.remove(x): x not in list exception.

Trả về giá trị từ Remove ()

list.remove(element)
2 không trả về bất kỳ giá trị nào (trả về
list.remove(element)
7).


Ví dụ 1: Xóa phần tử khỏi danh sách

# animals list
animals = ['cat', 'dog', 'rabbit', 'guinea pig']

# 'rabbit' is removed animals.remove('rabbit')

# Updated animals List print('Updated animals list: ', animals)

Đầu ra

Updated animals list:  ['cat', 'dog', 'guinea pig']

Ví dụ 2: Remove () phương thức trên danh sách có các phần tử trùng lặp

Nếu một danh sách chứa các phần tử trùng lặp, phương thức

list.remove(element)
2 chỉ xóa phần tử khớp đầu tiên.

# animals list
animals = ['cat', 'dog', 'dog', 'guinea pig', 'dog']

# 'dog' is removed animals.remove('dog')

# Updated animals list print('Updated animals list: ', animals)

Đầu ra

Updated animals list:  ['cat', 'dog', 'guinea pig', 'dog']

Ví dụ 2: Remove () phương thức trên danh sách có các phần tử trùng lặp


Nếu một danh sách chứa các phần tử trùng lặp, phương thức list.remove(element)2 chỉ xóa phần tử khớp đầu tiên.

# animals list
animals = ['cat', 'dog', 'rabbit', 'guinea pig']

# Deleting 'fish' element animals.remove('fish')

# Updated animals List print('Updated animals list: ', animals)

Đầu ra

Traceback (most recent call last):
  File ".. .. ..", line 5, in <module>
    animal.remove('fish')
ValueError: list.remove(x): x not in list

Ví dụ 2: Remove () phương thức trên danh sách có các phần tử trùng lặp


  • Nếu một danh sách chứa các phần tử trùng lặp, phương thức
    list.remove(element)
    2 chỉ xóa phần tử khớp đầu tiên.
  • Ở đây, chỉ có sự xuất hiện đầu tiên của phần tử 'con chó' được xóa khỏi danh sách.

Làm cách nào để loại bỏ một giá trị trong Python?

Phương thức Remove () xóa phần tử khớp đầu tiên (được truyền dưới dạng đối số) khỏi danh sách. Phương thức pop () loại bỏ một phần tử tại một chỉ mục nhất định và cũng sẽ trả về mục đã xóa. Bạn cũng có thể sử dụng từ khóa DEL trong Python để xóa một phần tử hoặc lát khỏi danh sách.

Làm cách nào để loại bỏ một cái gì đó từ một python được đặt?

  • Phương thức đặt python Remove () Phương thức Remove () Xóa phần tử được chỉ định khỏi tập hợp. Phương thức này khác với phương thức DISCARD (), vì phương thức Remove () sẽ gây ra lỗi nếu mục được chỉ định không tồn tại và phương thức DISCARD () sẽ không.
  • Xem thảo luận
  • Làm cách nào để loại bỏ một giá trị trong Python?

    Phương thức Remove () xóa phần tử khớp đầu tiên (được truyền dưới dạng đối số) khỏi danh sách. Phương thức pop () loại bỏ một phần tử tại một chỉ mục nhất định và cũng sẽ trả về mục đã xóa. Bạn cũng có thể sử dụng từ khóa DEL trong Python để xóa một phần tử hoặc lát khỏi danh sách.

    Làm cách nào để loại bỏ một cái gì đó từ một python được đặt?

    Phương thức đặt python Remove () Phương thức Remove () Xóa phần tử được chỉ định khỏi tập hợp. Phương thức này khác với phương thức DISCARD (), vì phương thức Remove () sẽ gây ra lỗi nếu mục được chỉ định không tồn tại và phương thức DISCARD () sẽ không.remove() is an inbuilt function in the Python programming language that removes a given object from the List. 

    Syntax: 

    list_name.remove(obj) 

    Parameters:    

    • Xem thảo luận object to be removed from the list 

    Returns:   

    Cải thiện bài viết

    Exception:

    Lưu bài viết

    Note:  

    Đọc

    Ví dụ 1: Xóa phần tử khỏi danh sách & nbsp; & nbsp;Remove element from the list  

    Python3

    Các

    Updated animals list:  ['cat', 'dog', 'guinea pig']
    6
    # animals list
    animals = ['cat', 'dog', 'rabbit', 'guinea pig']
    
    

    # 'rabbit' is removed animals.remove('rabbit')

    # Updated animals List print('Updated animals list: ', animals)
    4
    Updated animals list:  ['cat', 'dog', 'guinea pig']
    8

    Updated animals list:  ['cat', 'dog', 'guinea pig']
    9
    # animals list
    animals = ['cat', 'dog', 'dog', 'guinea pig', 'dog']
    
    

    # 'dog' is removed animals.remove('dog')

    # Updated animals list print('Updated animals list: ', animals)
    0

    Các

    Updated animals list:  ['cat', 'dog', 'guinea pig', 'dog']
    2
    # animals list
    animals = ['cat', 'dog', 'dog', 'guinea pig', 'dog']
    
    

    # 'dog' is removed animals.remove('dog')

    # Updated animals list print('Updated animals list: ', animals)
    4
    Updated animals list:  ['cat', 'dog', 'guinea pig']
    8

    Updated animals list:  ['cat', 'dog', 'guinea pig']
    9
    Updated animals list:  ['cat', 'dog', 'guinea pig', 'dog']
    6

    Đầu ra

    [2, 1, 1, 4, 5]
    ['b', 'c', 'd']
    

    Ví dụ 2: & nbsp; xóa phần tử không tồn tại & nbsp; & nbsp;Deleting element that doesn’t exist  

    Python3

    Các

    Updated animals list:  ['cat', 'dog', 'guinea pig', 'dog']
    2
    # animals list
    animals = ['cat', 'dog', 'rabbit', 'guinea pig']
    
    

    # Deleting 'fish' element animals.remove('fish')

    # Updated animals List print('Updated animals list: ', animals)
    9
    Updated animals list:  ['cat', 'dog', 'guinea pig']
    8

    Đầu ra

    Output:  

    list.remove(element)
    0

    Ví dụ 2: & nbsp; xóa phần tử không tồn tại & nbsp; & nbsp;remove() Method On a List having Duplicate Elements  

    Python3

    Updated animals list:  ['cat', 'dog', 'guinea pig']
    9
    Updated animals list:  ['cat', 'dog', 'guinea pig', 'dog']
    6

    Updated animals list:  ['cat', 'dog', 'guinea pig', 'dog']
    2
    Updated animals list:  ['cat', 'dog', 'guinea pig', 'dog']
    0
    Updated animals list:  ['cat', 'dog', 'guinea pig']
    8

    Updated animals list:  ['cat', 'dog', 'guinea pig']
    9
    Updated animals list:  ['cat', 'dog', 'guinea pig', 'dog']
    6

    Đầu ra

    list.remove(element)
    1

    Lưu ý: Nếu một danh sách chứa các phần tử trùng lặp, nó sẽ loại bỏ sự xuất hiện đầu tiên của đối tượng khỏi danh sách. & NBSP;: If a list contains duplicate elements, it removes the first occurrence of the object from the list. 

    Ví dụ 4: Cho một danh sách, xóa tất cả 1 1 khỏi danh sách và in danh sách

    Python3

    Các

    list.remove(element)
    16
    list.remove(element)
    17
    # animals list
    animals = ['cat', 'dog', 'rabbit', 'guinea pig']
    
    

    # 'rabbit' is removed animals.remove('rabbit')

    # Updated animals List print('Updated animals list: ', animals)
    4
    list.remove(element)
    19

    list.remove(element)
    20
    Updated animals list:  ['cat', 'dog', 'guinea pig']
    6
    # animals list
    animals = ['cat', 'dog', 'rabbit', 'guinea pig']
    
    

    # 'rabbit' is removed animals.remove('rabbit')

    # Updated animals List print('Updated animals list: ', animals)
    4
    Updated animals list:  ['cat', 'dog', 'guinea pig']
    8

    Updated animals list:  ['cat', 'dog', 'guinea pig']
    9
    # animals list
    animals = ['cat', 'dog', 'dog', 'guinea pig', 'dog']
    
    

    # 'dog' is removed animals.remove('dog')

    # Updated animals list print('Updated animals list: ', animals)
    0

    Ví dụ 5: Cho một danh sách, xóa tất cả 2 2 khỏi danh sách bằng cách sử dụng từ khóa & nbsp;

    Python3

    Các

    list.remove(element)
    16
    # animals list
    animals = ['cat', 'dog', 'rabbit', 'guinea pig']
    
    

    # 'rabbit' is removed animals.remove('rabbit')

    # Updated animals List print('Updated animals list: ', animals)
    6
    list.remove(element)
    41
    list.remove(element)
    42

    list.remove(element)
    20
    list.remove(element)
    44
    # animals list
    animals = ['cat', 'dog', 'rabbit', 'guinea pig']
    
    

    # 'rabbit' is removed animals.remove('rabbit')

    # Updated animals List print('Updated animals list: ', animals)
    6
    Updated animals list:  ['cat', 'dog', 'guinea pig']
    8

    Updated animals list:  ['cat', 'dog', 'guinea pig']
    9
    list.remove(element)
    48

    Lớp phức tạp: & nbsp;

    • Trường hợp trung bình: O (n)
    • Trường hợp xấu nhất được khấu hao: O (n)

    Có một chức năng xóa trong Python?

    Phương thức Remove () - Tổng quan về cú pháp Xóa () là một trong những cách bạn có thể xóa các phần tử khỏi danh sách trong Python. Phương thức Remove () xóa một mục khỏi danh sách theo giá trị của nó chứ không phải bằng số chỉ mục của nó.The remove() method is one of the ways you can remove elements from a list in Python. The remove() method removes an item from a list by its value and not by its index number.

    Làm thế nào loại bỏ phương thức hoạt động trong Python?

    Phương thức Remove () lấy một phần tử duy nhất làm đối số và loại bỏ nó khỏi danh sách.Nếu phần tử không tồn tại, nó sẽ ném valueError: list.remove (x): x không nằm trong ngoại lệ danh sách.takes a single element as an argument and removes it from the list. If the element doesn't exist, it throws ValueError: list.remove(x): x not in list exception.

    Làm cách nào để loại bỏ một giá trị trong Python?

    Phương thức Remove () xóa phần tử khớp đầu tiên (được truyền dưới dạng đối số) khỏi danh sách.Phương thức pop () loại bỏ một phần tử tại một chỉ mục nhất định và cũng sẽ trả về mục đã xóa.Bạn cũng có thể sử dụng từ khóa DEL trong Python để xóa một phần tử hoặc lát khỏi danh sách.

    Làm cách nào để loại bỏ một cái gì đó từ một python được đặt?

    Phương thức đặt python Remove () Phương thức Remove () Xóa phần tử được chỉ định khỏi tập hợp.Phương thức này khác với phương thức DISCARD (), vì phương thức Remove () sẽ gây ra lỗi nếu mục được chỉ định không tồn tại và phương thức DISCARD () sẽ không.The remove() method removes the specified element from the set. This method is different from the discard() method, because the remove() method will raise an error if the specified item does not exist, and the discard() method will not.