python 中的pop()
函數有助於從集合中移除任意元素。它還將移除的元素作為輸出返回。
**set.pop()**
pop()
函數不接受任何參數。它通過移除返回元素來更新集合。
這個函數的返回值是集合中的一個隨機元素。如果集合為空,此函數將引發類型錯誤異常。
| 投入 | 返回值 |
| 如果設置 | 隨機元素 |
X ={1, 2, 3, 4}
print('Return Value is', X.pop())
print('X = ', X)
輸出:
Return Value is 4
A = {1, 2, 3}
# on an empty set
X = {}
# Popping elements and printing them
print(X.pop())
# The updated set
print("Updated set is", X)
輸出:
Traceback (most recent call last):
File "/home/7c5b1d5728eb9aa0e63b1d70ee5c410e.py", line 6, in
print(X.pop())
TypeError: pop expected at least 1 arguments, got 0
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/127132.html