Hướng dẫn which method is called in a pop () method python? - phương thức nào được gọi trong phương thức pop() python?

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

Phương thức

list.pop(index)
0 loại bỏ mục tại chỉ mục đã cho ra khỏi danh sách và trả về mục đã xóa.

Thí dụ

# create a list of prime numbers
prime_numbers = [2, 3, 5, 7]

# remove the element at index 2
removed_element = prime_numbers.pop(2)

print('Removed Element:', removed_element)
print('Updated List:', prime_numbers)

# Output: 
# Removed Element: 5
# Updated List: [2, 3, 7]


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

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

list.pop(index)
0 là:

list.pop(index)

tham số pop ()

  • Phương thức
    list.pop(index)
    0 có một đối số duy nhất (chỉ mục).
  • Đối số được truyền cho phương pháp là tùy chọn. Nếu không được thông qua, chỉ mục mặc định -1 được truyền dưới dạng đối số (chỉ mục của mục cuối cùng).-1 is passed as an argument (index of the last item).
  • Nếu chỉ mục được truyền vào phương thức không nằm trong phạm vi, nó sẽ ném IndexError: Pop Index ra khỏi phạm vi ngoại lệ.IndexError: pop index out of range exception.

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

Phương thức

list.pop(index)
0 trả về mục có mặt tại chỉ mục đã cho. Mục này cũng bị xóa khỏi danh sách.


Ví dụ 1: Mục pop tại chỉ mục đã cho từ danh sách

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)

Đầu ra

Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']

Lưu ý: Chỉ mục trong Python bắt đầu từ 0, không phải 1. Index in Python starts from 0, not 1.

Nếu bạn cần bật phần tử thứ 4, bạn cần chuyển 3 cho phương thức

list.pop(index)
0.3 to the
list.pop(index)
0 method.


Ví dụ 2: pop () không có chỉ mục và cho các chỉ số âm

# programming languages list
languages = ['Python', 'Java', 'C++', 'Ruby', 'C']

# remove and return the last item
print('When index is not passed:') 

print('Return Value:', languages.pop())

print('Updated List:', languages) # remove and return the last item print('\nWhen -1 is passed:')

print('Return Value:', languages.pop(-1))

print('Updated List:', languages) # remove and return the third last item print('\nWhen -3 is passed:')

print('Return Value:', languages.pop(-3))

print('Updated List:', languages)

Đầu ra

When index is not passed:
Return Value: C
Updated List: ['Python', 'Java', 'C++', 'Ruby']

When -1 is passed:
Return Value: Ruby
Updated List: ['Python', 'Java', 'C++']

When -3 is passed:
Return Value: Python
Updated List: ['Java', 'C++']

Lưu ý: Chỉ mục trong Python bắt đầu từ 0, không phải 1.

Nếu bạn cần bật phần tử thứ 4, bạn cần chuyển 3 cho phương thức

list.pop(index)
0.

Python Danh sách pop () là một hàm sẵn có trong Python loại bỏ và trả về giá trị cuối cùng từ danh sách hoặc giá trị chỉ mục đã cho.pop()is an inbuilt function in Python that removes and returns the last value from the List or the given index value.

Python danh sách pop () Phương thức cú pháp

Cú pháp: list_name.pop (index)list_name.pop(index)

  • Chỉ mục (Tùy chọn) - Giá trị tại Index được bật ra và xóa. Nếu chỉ mục không được đưa ra, thì phần tử cuối cùng được bật ra và xóa.(optional) – The value at index is popped out and removed. If the index is not given, then the last element is popped out and removed.

Trả về: Trả về giá trị cuối cùng hoặc giá trị chỉ mục đã cho từ danh sách.ReturnsThe last value or the given index value from the list.

Ngoại lệ: Tăng IndexErrorWhen chỉ số nằm ngoài phạm vi.Raises IndexErrorWhen the index is out of range.

Ví dụ về phương thức python danh sách pop ()

Python3

list.pop(index)
6
list.pop(index)
7
list.pop(index)
8
list.pop(index)
9
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
0
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
1
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
0
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
3
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
0
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
5
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
6

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
7
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
8
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
9
Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']
0

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
7
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
8
Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']
3
Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']
4

Output:

Popped element: 4
List after pop(): [1, 2, 3]

Ví dụ 1: Sử dụng phương thức danh sách pop () để bật một phần tử tại chỉ mục đã cho

Python3

Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']
5
list.pop(index)
7
list.pop(index)
8
list.pop(index)
9
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
0
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
1
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
0
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
3__

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
7
When index is not passed:
Return Value: C
Updated List: ['Python', 'Java', 'C++', 'Ruby']

When -1 is passed:
Return Value: Ruby
Updated List: ['Python', 'Java', 'C++']

When -3 is passed:
Return Value: Python
Updated List: ['Java', 'C++']
1

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
7
When index is not passed:
Return Value: C
Updated List: ['Python', 'Java', 'C++', 'Ruby']

When -1 is passed:
Return Value: Ruby
Updated List: ['Python', 'Java', 'C++']

When -3 is passed:
Return Value: Python
Updated List: ['Java', 'C++']
3
When index is not passed:
Return Value: C
Updated List: ['Python', 'Java', 'C++', 'Ruby']

When -1 is passed:
Return Value: Ruby
Updated List: ['Python', 'Java', 'C++']

When -3 is passed:
Return Value: Python
Updated List: ['Java', 'C++']
4
When index is not passed:
Return Value: C
Updated List: ['Python', 'Java', 'C++', 'Ruby']

When -1 is passed:
Return Value: Ruby
Updated List: ['Python', 'Java', 'C++']

When -3 is passed:
Return Value: Python
Updated List: ['Java', 'C++']
5

Output:

6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]

Ví dụ 2: Chứng minh Indexerror & NBSP;DemonstratingIndexError 

Python3

Các

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
7
When index is not passed:
Return Value: C
Updated List: ['Python', 'Java', 'C++', 'Ruby']

When -1 is passed:
Return Value: Ruby
Updated List: ['Python', 'Java', 'C++']

When -3 is passed:
Return Value: Python
Updated List: ['Java', 'C++']
3
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
3
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
4

Output:  

Traceback (most recent call last):
  File "/home/1875538d94d5aecde6edea47b57a2212.py", line 5, in 
    print(list1.pop(8))
IndexError: pop index out of range

Ví dụ 3: Ví dụ thực tế

Một danh sách các loại trái cây chứa trái cây và tài sản cho biết trái cây của nó. Danh sách khác Tiêu thụ có hai mặt hàng nước trái cây và ăn. Với sự trợ giúp của pop () và append (), chúng tôi có thể làm điều gì đó thú vị. & Nbsp;

Python3

6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
5
list.pop(index)
7
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
77____78
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
9
Traceback (most recent call last):
  File "/home/1875538d94d5aecde6edea47b57a2212.py", line 5, in 
    print(list1.pop(8))
IndexError: pop index out of range
0
Traceback (most recent call last):
  File "/home/1875538d94d5aecde6edea47b57a2212.py", line 5, in 
    print(list1.pop(8))
IndexError: pop index out of range
1____822

[['Orange', 'Fruit', 'Juice'], ['Orange', 'Fruit', 'Eat'],
 ['Banana', 'Fruit', 'Juice'], ['Banana', 'Fruit', 'Eat'],
 ['Mango', 'Fruit', 'Juice'], ['Mango', 'Fruit', 'Eat']]
0
list.pop(index)
7
list.pop(index)
8
[['Orange', 'Fruit', 'Juice'], ['Orange', 'Fruit', 'Eat'],
 ['Banana', 'Fruit', 'Juice'], ['Banana', 'Fruit', 'Eat'],
 ['Mango', 'Fruit', 'Juice'], ['Mango', 'Fruit', 'Eat']]
3
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
0
[['Orange', 'Fruit', 'Juice'], ['Orange', 'Fruit', 'Eat'],
 ['Banana', 'Fruit', 'Juice'], ['Banana', 'Fruit', 'Eat'],
 ['Mango', 'Fruit', 'Juice'], ['Mango', 'Fruit', 'Eat']]
5
# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
6

[['Orange', 'Fruit', 'Juice'], ['Orange', 'Fruit', 'Eat'],
 ['Banana', 'Fruit', 'Juice'], ['Banana', 'Fruit', 'Eat'],
 ['Mango', 'Fruit', 'Juice'], ['Mango', 'Fruit', 'Eat']]
7
list.pop(index)
7
[['Orange', 'Fruit', 'Juice'], ['Orange', 'Fruit', 'Eat'],
 ['Banana', 'Fruit', 'Juice'], ['Banana', 'Fruit', 'Eat'],
 ['Mango', 'Fruit', 'Juice'], ['Mango', 'Fruit', 'Eat']]
9

list.pop(index)
00
list.pop(index)
01
list.pop(index)
02
list.pop(index)
03

list.pop(index)
04
list.pop(index)
00
list.pop(index)
06
list.pop(index)
02
list.pop(index)
08

list.pop(index)
09
list.pop(index)
10

list.pop(index)
09
list.pop(index)
12

list.pop(index)
09
list.pop(index)
14
list.pop(index)
15
list.pop(index)
9
list.pop(index)
17

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']

# remove and return the 4th item return_value = languages.pop(3)

print('Return Value:', return_value) # Updated List print('Updated List:', languages)
7
list.pop(index)
19

Output: 

[['Orange', 'Fruit', 'Juice'], ['Orange', 'Fruit', 'Eat'],
 ['Banana', 'Fruit', 'Juice'], ['Banana', 'Fruit', 'Eat'],
 ['Mango', 'Fruit', 'Juice'], ['Mango', 'Fruit', 'Eat']]

Độ phức tạp về thời gian: & nbsp;

Độ phức tạp của tất cả các ví dụ trên là không đổi o (1) trong cả trường hợp trung bình và được khấu hao & nbsp;


Làm thế nào để pop () hoạt động 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.

Tính năng của hàm pop () là gì?

Đặc điểm của ngôn ngữ lập trình theo định hướng thủ tục: Nó nhấn mạnh vào thuật toán (thực hiện việc này).Các chương trình lớn được chia thành các chương trình nhỏ hơn được gọi là chức năng.Chức năng có thể giao tiếp bằng biến toàn cầu.Dữ liệu di chuyển tự do từ chức năng này sang chức năng khác.It emphasis on algorithm (doing this ). Large programs are divided into smaller programs known as functions. Function can communicate by global variable. Data move freely from one function to another function.