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
0 có một đối số duy nhất [chỉ mục].list.pop[index]
- Đố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]
6list.pop[index]
7 list.pop[index]
8list.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]
9Return 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]
8Return 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]
8list.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]
7When 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]
7When 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]
7When 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 range0
Traceback [most recent call last]: File "/home/1875538d94d5aecde6edea47b57a2212.py", line 5, in print[list1.pop[8]] IndexError: pop index out of range1____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]
01list.pop[index]
02 list.pop[index]
03list.pop[index]
04list.pop[index]
00 list.pop[index]
06list.pop[index]
02 list.pop[index]
08list.pop[index]
09list.pop[index]
10list.pop[index]
09list.pop[index]
12list.pop[index]
09list.pop[index]
14list.pop[index]
15list.pop[index]
9list.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]
7list.pop[index]
19Output:
[['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;