Hướng dẫn pop and del in python - pop và del trong python

Hiệu ứng của ba phương pháp khác nhau để loại bỏ một phần tử khỏi danh sách:

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
1 Xóa giá trị khớp đầu tiên, không phải là một chỉ mục cụ thể:

>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
2 Xóa vật phẩm tại một chỉ mục cụ thể:

>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
3 loại bỏ mục tại một chỉ mục cụ thể và trả về nó.

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]

Các chế độ lỗi của họ cũng khác nhau:

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range

Hướng dẫn pop and del in python - pop và del trong python

MIT

10,9k10 Huy hiệu vàng48 Huy hiệu bạc74 Huy hiệu đồng10 gold badges48 silver badges74 bronze badges

Đã trả lời ngày 17 tháng 7 năm 2012 lúc 10:24Jul 17, 2012 at 10:24

Martijn Pieters ♦ Martijn PietersMartijn Pieters

997K279 Huy hiệu vàng3924 Huy hiệu bạc3264 Huy hiệu đồng279 gold badges3924 silver badges3264 bronze badges

11

Sử dụng

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
2 để xóa một phần tử theo chỉ mục,
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
5 để xóa nó theo chỉ mục nếu bạn cần giá trị trả về và
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
6 để xóa một phần tử theo giá trị. Lần cuối cùng yêu cầu tìm kiếm danh sách và tăng
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
7 nếu không có giá trị như vậy xảy ra trong danh sách.

Khi xóa chỉ mục

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
8 khỏi danh sách các yếu tố
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
9, sự phức tạp tính toán của các phương pháp này là

del     O(n - i)
pop     O(n - i)
remove  O(n)

Hướng dẫn pop and del in python - pop và del trong python

jtlz2

6.8297 Huy hiệu vàng60 Huy hiệu bạc102 Huy hiệu Đồng7 gold badges60 silver badges102 bronze badges

Đã trả lời ngày 17 tháng 7 năm 2012 lúc 10:25Jul 17, 2012 at 10:25

Sven Marnachsven MarnachSven Marnach

550K114 Huy hiệu vàng920 Huy hiệu bạc822 Huy hiệu Đồng114 gold badges920 silver badges822 bronze badges

7

Vì không có ai khác đã đề cập đến nó, lưu ý rằng

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
2 (không giống như
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
3) cho phép loại bỏ một loạt các chỉ mục do cắt danh sách:

>>> lst = [3, 2, 2, 1]
>>> del lst[1:]
>>> lst
[3]

Điều này cũng cho phép tránh

del     O(n - i)
pop     O(n - i)
remove  O(n)
2 nếu chỉ mục không có trong danh sách:

>>> lst = [3, 2, 2, 1]
>>> del lst[10:]
>>> lst
[3, 2, 2, 1]

Đã trả lời ngày 16 tháng 1 năm 2017 lúc 10:27Jan 16, 2017 at 10:27

Chris_RandsChris_RandsChris_Rands

36.8K13 Huy hiệu vàng80 Huy hiệu bạc112 Huy hiệu đồng13 gold badges80 silver badges112 bronze badges

Đã trả lời khá tốt bởi những người khác. Cái này từ cuối của tôi :)

Hướng dẫn pop and del in python - pop và del trong python

Rõ ràng,

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
3 là người duy nhất trả về giá trị và
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
1 là người duy nhất tìm kiếm đối tượng, trong khi
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
2 tự giới hạn trong việc xóa đơn giản.

Đã trả lời ngày 8 tháng 8 năm 2017 lúc 15:33Aug 8, 2017 at 15:33

Saurav Sahusaurav SahuSaurav Sahu

12.2k5 Huy hiệu vàng56 Huy hiệu bạc75 Huy hiệu Đồng5 gold badges56 silver badges75 bronze badges

1

Nhiều lời giải thích tốt là ở đây nhưng tôi sẽ cố gắng hết sức để đơn giản hóa nhiều hơn.

Trong số tất cả các phương pháp này,

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
1 &
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
3 là postfix trong khi xóa là tiền tố.postfix while delete is prefix.

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
6: được sử dụng để loại bỏ sự xuất hiện đầu tiên của phần tử.
del     O(n - i)
pop     O(n - i)
remove  O(n)
9 => Sự xuất hiện đầu tiên của
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
9 trong danh sách.
Is used to remove first occurrence of element.
del     O(n - i)
pop     O(n - i)
remove  O(n)
9 => first occurrence of
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
9 in the list.

>>> a = [0, 2, 3, 2, 1, 4, 6, 5, 7]
>>> a.remove(2)   # where i = 2
>>> a
[0, 3, 2, 1, 4, 6, 5, 7]

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
5: Được sử dụng để loại bỏ phần tử ... Is used to remove element ...

  • Nếu không có chỉ mục được chỉ định:
    >>> a = [4, 5, 6]
    >>> a.remove(7)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: list.remove(x): x not in list
    >>> del a[7]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: list assignment index out of range
    >>> a.pop(7)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: pop index out of range
    
    5 => từ cuối danh sách
    >>> a = [4, 5, 6]
    >>> a.remove(7)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: list.remove(x): x not in list
    >>> del a[7]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: list assignment index out of range
    >>> a.pop(7)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: pop index out of range
    
    5 => from end of list
>>> a.pop()
>>> a
[0, 3, 2, 1, 4, 6, 5]
  • Nếu một chỉ mục được chỉ định:
    >>> lst = [3, 2, 2, 1]
    >>> del lst[1:]
    >>> lst
    [3]
    
    3 => của chỉ mục
    >>> lst = [3, 2, 2, 1]
    >>> del lst[1:]
    >>> lst
    [3]
    
    3 => of index
>>> a.pop(2)
>>> a
[0, 3, 1, 4, 6, 5]

Cảnh báo: Phương pháp nguy hiểm phía trước

>>> lst = [3, 2, 2, 1]
>>> del lst[1:]
>>> lst
[3]
4: Đó là một phương thức tiền tố.: It's a prefix method.

Theo dõi hai cú pháp khác nhau cho cùng một phương pháp: với

>>> lst = [3, 2, 2, 1]
>>> del lst[1:]
>>> lst
[3]
5 và không có. Nó sở hữu sức mạnh để:

  • Xóa chỉ mục
    >>> lst = [3, 2, 2, 1]
    >>> del lst[1:]
    >>> lst
    [3]
    
    6 => Được sử dụng để xóa theo chỉ mục và giá trị liên quan của nó giống như
    >>> a = [4, 5, 6]
    >>> a.remove(7)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: list.remove(x): x not in list
    >>> del a[7]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: list assignment index out of range
    >>> a.pop(7)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: pop index out of range
    
    3.
    >>> lst = [3, 2, 2, 1]
    >>> del lst[1:]
    >>> lst
    [3]
    
    6 => used to delete by index and its associated value just like
    >>> a = [4, 5, 6]
    >>> a.remove(7)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: list.remove(x): x not in list
    >>> del a[7]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: list assignment index out of range
    >>> a.pop(7)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: pop index out of range
    
    3.
>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
0
  • Xóa các giá trị trong phạm vi
    >>> lst = [3, 2, 2, 1]
    >>> del lst[1:]
    >>> lst
    [3]
    
    8:
    >>> lst = [3, 2, 2, 1]
    >>> del lst[1:]
    >>> lst
    [3]
    
    9 => Nhiều giá trị trong phạm vi.
    >>> lst = [3, 2, 2, 1]
    >>> del lst[1:]
    >>> lst
    [3]
    
    9 => multiple values in range.
>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
1
  • Cuối cùng nhưng không kém phần quan trọng, để xóa toàn bộ danh sách trong một lần bắn.
    >>> lst = [3, 2, 2, 1]
    >>> del lst[10:]
    >>> lst
    [3, 2, 2, 1]
    
    0 => Như đã nói ở trên.
    >>> lst = [3, 2, 2, 1]
    >>> del lst[10:]
    >>> lst
    [3, 2, 2, 1]
    
    0 => as said above.
>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
2

Hy vọng điều này làm rõ sự nhầm lẫn.

Đã trả lời ngày 26 tháng 8 năm 2018 lúc 15:34Aug 26, 2018 at 15:34

2

nhạc pop

Lấy chỉ mục (khi được đưa ra, khác lấy lần cuối), xóa giá trị ở chỉ mục đó và trả về giá trị

gỡ bỏ

Mất giá trị, loại bỏ lần xuất hiện đầu tiên và không trả về không có gì

delete

Lấy chỉ mục, xóa giá trị ở chỉ mục đó và không trả về không có gì

Hướng dẫn pop and del in python - pop và del trong python

ack

1.9582 huy hiệu vàng18 Huy hiệu bạc24 Huy hiệu đồng2 gold badges18 silver badges24 bronze badges

Đã trả lời ngày 7 tháng 2 năm 2015 lúc 6:51Feb 7, 2015 at 6:51

Bahubali Patilbahubali PatilBahubali Patil

1.0871 Huy hiệu vàng16 Huy hiệu bạc22 Huy hiệu đồng1 gold badge16 silver badges22 bronze badges

1

Bất kỳ hoạt động/chức năng nào trên các cấu trúc dữ liệu khác nhau được xác định cho các hành động cụ thể. Ở đây trong trường hợp của bạn, tức là loại bỏ một phần tử, xóa, bật và xóa. (Nếu bạn xem xét các bộ, thêm một thao tác khác - loại bỏ) Trường hợp khó hiểu khác là trong khi thêm. Chèn/nối. Để trình diễn, chúng ta hãy thực hiện Deque. Deque là một cấu trúc dữ liệu tuyến tính lai, trong đó bạn có thể thêm các phần tử / loại bỏ các phần tử khỏi cả hai đầu. (Phía sau và phía trước)

>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
3

Ở đây, xem các hoạt động:

>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
4

Hoạt động phải trả lại một cái gì đó. Vì vậy, pop - có và không có chỉ mục. Nếu tôi không muốn trả về giá trị: del self.items [0]

Xóa theo giá trị Không chỉ mục:

  • gỡ bỏ :

    >>> a = [9, 8, 7, 6]
    >>> del a[1]
    >>> a
    [9, 7, 6]
    
    5

Trả lại [1,3,5,7]

Hãy để chúng tôi xem xét trường hợp của các bộ.

>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
6

Đã trả lời ngày 10 tháng 7 năm 2017 lúc 17:29Jul 10, 2017 at 17:29

Phanindravarmaphanindravarmaphanindravarma

1.2171 Huy hiệu vàng9 Huy hiệu bạc8 Huy hiệu đồng1 gold badge9 silver badges8 bronze badges

Đây là một câu trả lời chi tiết.

DEL có thể được sử dụng cho bất kỳ đối tượng lớp nào trong khi POP và xóa và giới hạn với các lớp cụ thể.

Cho

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
2

Dưới đây là một số ví dụ

>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
7

Chúng ta có thể ghi đè phương thức

>>> lst = [3, 2, 2, 1]
>>> del lst[10:]
>>> lst
[3, 2, 2, 1]
2 trong các lớp do người dùng tạo.

Sử dụng cụ thể với danh sách

>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
8

Cho

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
3

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
3 lấy chỉ mục làm tham số và xóa phần tử tại chỉ mục đó

Không giống như

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
2,
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
3 khi được gọi trên đối tượng danh sách trả về giá trị tại chỉ mục đó

>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
9

Cho

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
1

Xóa lấy giá trị tham số và xóa giá trị đó khỏi danh sách.

Nếu có nhiều giá trị có mặt sẽ loại bỏ lần xuất hiện đầu tiên

>>> lst = [3, 2, 2, 1]
>>> del lst[10:]
>>> lst
[3, 2, 2, 1]
8: sẽ ném giá trịerRor nếu giá trị đó không có mặt

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
0

Hy vọng câu trả lời này là hữu ích.

Đã trả lời ngày 9 tháng 10 năm 2020 lúc 13:17Oct 9, 2020 at 13:17

Hướng dẫn pop and del in python - pop và del trong python

NitishnitishNitish

5654 Huy hiệu bạc16 Huy hiệu đồng4 silver badges16 bronze badges

Hoạt động xóa trong danh sách được cung cấp một giá trị để xóa. Nó tìm kiếm danh sách để tìm một mục có giá trị đó và xóa mục khớp đầu tiên mà nó tìm thấy. Đó là một lỗi nếu không có mục phù hợp, tăng giá trị.

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
1

Câu lệnh DEL có thể được sử dụng để xóa toàn bộ danh sách. Nếu bạn có một mục danh sách cụ thể làm đối số của bạn với DEL (ví dụ: ListName [7] để tham khảo cụ thể mục thứ 8 trong danh sách), nó sẽ chỉ xóa mục đó. Thậm chí có thể xóa một "lát" khỏi danh sách. Đó là một lỗi nếu có chỉ mục ra khỏi phạm vi, tăng chỉ mục.

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
2

Việc sử dụng POP thông thường là xóa mục cuối cùng khỏi danh sách khi bạn sử dụng danh sách làm ngăn xếp. Không giống như Del, Pop trả về giá trị mà nó xuất hiện trong danh sách. Bạn có thể tùy chọn cung cấp giá trị chỉ mục cho Pop và Pop từ ngoài phần cuối của danh sách (ví dụ: listname.pop (0) sẽ xóa mục đầu tiên khỏi danh sách và trả về mục đầu tiên đó là kết quả của nó). Bạn có thể sử dụng điều này để làm cho danh sách hoạt động giống như một hàng đợi, nhưng có những thói quen thư viện có sẵn có thể cung cấp các hoạt động hàng đợi với hiệu suất tốt hơn pop (0). Đó là một lỗi nếu có chỉ mục ra khỏi phạm vi, tăng chỉ mục.

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
3

Xem bộ sưu tập.Deque để biết thêm chi tiết.

Đã trả lời ngày 9 tháng 8 năm 2018 lúc 17:13Aug 9, 2018 at 17:13

Hướng dẫn pop and del in python - pop và del trong python

Loại bỏ cơ bản hoạt động trên giá trị. Xóa và Pop hoạt động trên chỉ mục

Loại bỏ cơ bản loại bỏ giá trị khớp đầu tiên. Xóa Xóa mục khỏi một chỉ mục cụ thể pop về cơ bản lấy một chỉ mục và trả về giá trị tại chỉ mục đó. Lần tới khi bạn in danh sách giá trị không xuất hiện.

Hướng dẫn pop and del in python - pop và del trong python

Đã trả lời ngày 28 tháng 11 năm 2019 lúc 11:03Nov 28, 2019 at 11:03

Hướng dẫn pop and del in python - pop và del trong python

Harshal SGHARSHAL SGHarshal SG

3532 Huy hiệu bạc7 Huy hiệu Đồng2 silver badges7 bronze badges

1

Trong khi Pop và xóa cả hai đều lấy các chỉ số để xóa một phần tử như đã nêu trong các bình luận trên. Một sự khác biệt chính là sự phức tạp về thời gian cho họ. Độ phức tạp thời gian cho pop () không có chỉ số là O (1) nhưng không phải là trường hợp tương tự để xóa phần tử cuối cùng.

Nếu trường hợp sử dụng của bạn luôn luôn xóa phần tử cuối cùng, thì luôn luôn sử dụng pop () over Delete (). Để biết thêm lời giải thích về sự phức tạp về thời gian, bạn có thể tham khảo https://www.ics.uci.edu/~pattis/ics-33/lectures/complexitypython.txt

Đã trả lời ngày 4 tháng 9 năm 2016 lúc 20:22Sep 4, 2016 at 20:22

Hướng dẫn pop and del in python - pop và del trong python

1

loại bỏ (), del và pop () chậm ... còn 'không' thì sao?

Giữa rất nhiều phản hồi, tôi không thấy ai nói về hiệu suất. Vì vậy, tôi có một mẹo hiệu suất:

Xóa (), del và pop () Sau khi xóa di chuyển tất cả các giá trị còn lại sang trái ...

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
4

... Làm cho việc xử lý chậm!

Thay đổi giá trị mong muốn thành NULL để xử lý thêm xóa chỉ có thể thêm nhiều tốc độ vào chương trình của bạn, đặc biệt là khi xử lý một khối lượng lớn dữ liệu:

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
5

Tất nhiên, việc thiết lập giá trị null khác với việc loại bỏ nó, nhưng nếu bạn muốn hiểu thêm một chút về việc xóa, hãy nghĩ về hiệu suất của hoạt động này cũng có vẻ thú vị với tôi.

Đã trả lời ngày 14 tháng 10 lúc 23:32Oct 14 at 23:32

Hướng dẫn pop and del in python - pop và del trong python

Fellipe Sanchesfellipe SanchesFellipe Sanches

6.4264 Huy hiệu vàng30 Huy hiệu bạc 30 Huy hiệu Đồng4 gold badges30 silver badges30 bronze badges

Sự khác biệt giữa DEL, POP & Xóa về tốc độ thực hiện:

Trong khi loại bỏ bất kỳ mục trung gian nào:

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
6

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
2 vs
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
3 so với
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
1:

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
7

del () có vẻ nhanh hơn đáng kể so với hai cái còn lại, trong khi loại bỏ () là chậm nhất.

Trong khi loại bỏ mục cuối cùng:

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
8

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
2 vs
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
3 so với
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
1:

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
9

del () có vẻ nhanh hơn đáng kể so với hai cái còn lại, trong khi loại bỏ () là chậm nhất.

Trong khi loại bỏ mục cuối cùng:2 days ago

Hướng dẫn pop and del in python - pop và del trong python

del () và pop () mất thời gian loại bỏ mục cuối cùng.Ash Nazg

Đã trả lời 2 ngày trước2 gold badges4 silver badges12 bronze badges

Ash Nazgash Nazg

>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
0

3792 Huy hiệu vàng4 Huy hiệu bạc12 Huy hiệu đồng

Hướng dẫn pop and del in python - pop và del trong python

Bạn cũng có thể sử dụng Xóa để xóa một giá trị theo chỉ mục là tốt.Oct 16, 2012 at 0:41

3

Sự khác biệt giữa Pop và Del trong Python là gì?

Hàm pop () được sử dụng để trả về phần tử bị loại bỏ từ chức năng danh sách. Del () được sử dụng để xóa một phần tử tại một số chỉ mục được chỉ định trong danh sách. The del() function is used to delete an element at a specified index number in the list.

Làm thế nào là pop () khác với Remove ()?

Các phần tử mảng có thể được loại bỏ bằng phương thức pop () hoặc xóa ().Sự khác biệt giữa hai chức năng này là cái trước trả về giá trị bị xóa trong khi cái sau thì không.Hàm pop () không có tham số hoặc giá trị chỉ mục làm tham số của nó.the former returns the deleted value whereas the latter does not. The pop() function takes either no parameter or the index value as its parameter.

Là pop hay del python nhanh hơn?

POP chậm hơn vì 2 lý do: nó cần trả về một giá trị và nó cần tra cứu tên cho tên pop cho mỗi truy cập.Cảm ơn @anttihaapala.Trong ví dụ đầu tiên của bạn, bạn có del k (không có tác dụng đối với d) thay vì del d [k]. for 2 reasons: it needs to return a value, and it needs a name lookup for the pop name for each access. Thanks @AnttiHaapala. In your first example you have del k (which has no effect on d ) instead of del d[k] .

Pop được sử dụng cho những gì trong Python?

Danh sách Pop trong Python là một hàm được xác định trước, được xây dựng, loại bỏ một mục tại chỉ mục được chỉ định khỏi danh sách.Bạn cũng có thể sử dụng Pop trong Python mà không đề cập đến giá trị chỉ mục.Trong các trường hợp như vậy, hàm pop () sẽ loại bỏ phần tử cuối cùng của danh sách.removes an item at the specified index from the list. You can also use pop in Python without mentioning the index value. In such cases, the pop() function will remove the last element of the list.