Hướng dẫn what is data pop () in python? - dữ liệu pop() trong python là gì?

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

____10

6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
1
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
2
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
3
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
4
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
5
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
4
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
7
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
4
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
Traceback (most recent call last):
  File "/home/1875538d94d5aecde6edea47b57a2212.py", line 5, in 
    print(list1.pop(8))
IndexError: pop index out of range
2223
Traceback (most recent call last):
  File "/home/1875538d94d5aecde6edea47b57a2212.py", line 5, in 
    print(list1.pop(8))
IndexError: pop index out of range
4

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

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

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

Traceback (most recent call last):
  File "/home/1875538d94d5aecde6edea47b57a2212.py", line 5, in 
    print(list1.pop(8))
IndexError: pop index out of range
1
# 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]
5

Traceback (most recent call last):
  File "/home/1875538d94d5aecde6edea47b57a2212.py", line 5, in 
    print(list1.pop(8))
IndexError: pop index out of range
1
# 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]
7
# 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]
8
# 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]
9

Output:

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

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

Python3

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

Traceback (most recent call last):
  File "/home/1875538d94d5aecde6edea47b57a2212.py", line 5, in 
    print(list1.pop(8))
IndexError: pop index out of range
1
# 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]
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)
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

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

# 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
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
1
Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']
1
Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']
2
Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']
3
Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']
4
Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']
5
Return Value: French
Updated List: ['Python', 'Java', 'C++', 'C']
6
Return Value: French
Updated List: ['Python', 'Java', 'C++', '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++']
1
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
1
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
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++']
6
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++']
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++']
8
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
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
00
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++']
6
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
02

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

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

6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
03
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
08
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
09
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
3
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
11

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

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;


pop () là một phương thức của kiểu dữ liệu phức tạp được gọi là danh sách. Danh sách này là một trong những kiểu dữ liệu phức tạp được sử dụng phổ biến nhất trong Python và phương thức pop () chịu trách nhiệm bật một mục từ danh sách Python. Phương thức POP sẽ xóa một mục khỏi một chỉ mục nhất định của danh sách và trả về mục đã xóa.

Hình thức pop đầy đủ trong Python là gì?

Hình thức đầy đủ của pop là một điểm hiện diện hoặc giao thức bưu điện. Pop là một điểm phân định nơi nhiều thiết bị chia sẻ kết nối và giao tiếp với nhau.

# 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]


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ụ.

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

6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
14 là:

list.pop(index)

tham số pop ()

  • Phương thức
    6 [1, 2, 3, 4, 5]
    1 [2, 3, 4, 5]
    14 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

6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
14 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

6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
14.3 to the
6 [1, 2, 3, 4, 5]
1 [2, 3, 4, 5]
14 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

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

Mục đích của pop () là gì?

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.

Hoạt động pop trong Python là gì?

Phương thức python pop () loại bỏ một mục tại một giá trị chỉ mục được chỉ định khỏi danh sách.Phương thức này trả về mục bạn đã xóa để bạn biết sửa đổi nào đã được thực hiện trong danh sách của bạn.pop () chấp nhận một đối số: chỉ mục của mục bạn muốn xóa.

Phương thức nào được gọi trong một phương thức pop () python?

pop () là một phương thức của kiểu dữ liệu phức tạp được gọi là danh sách.Danh sách này là một trong những kiểu dữ liệu phức tạp được sử dụng phổ biến nhất trong Python và phương thức pop () chịu trách nhiệm bật một mục từ danh sách Python.Phương thức POP sẽ xóa một mục khỏi một chỉ mục nhất định của danh sách và trả về mục đã xóa.complex datatype called list. The list is among the most commonly used complex datatype in python, and the pop() method is responsible for popping an item from the python list. The pop method will remove an item from a given index of the list and returns the removed item.

Hình thức pop đầy đủ trong Python là gì?

Hình thức đầy đủ của pop là một điểm hiện diện hoặc giao thức bưu điện.Pop là một điểm phân định nơi nhiều thiết bị chia sẻ kết nối và giao tiếp với nhau.a Point of Presence Or Post Office Protocol. POP is a demarcation point where many devices share a connection and communicate with each other.