新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythondiscard()
python 中的discard()函數(shù)有助于在元素存在的情況下從集合中移除或丟棄指定的元素。

**s.discard(element)** #where element may be a integer,string etc.
丟棄()參數(shù):
discard()函數(shù)接受一個(gè)參數(shù)。此方法類似于remove()方法,不同之處在于,如果給定元素不在集合中,discard()方法會(huì)引發(fā)錯(cuò)誤,但remove()不會(huì)。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 元素 | 要搜索和移除的項(xiàng)目 | 需要 |
丟棄()返回值
discard()方法不返回任何內(nèi)容。它只是通過(guò)從集合中移除指定的元素來(lái)更新集合。
Python 中discard()方法的示例
示例discard()在 Python 中是如何工作的?
alphabets = {'a', 'b', 'c', 'd'}
alphabets .discard(c)
print('Alphabets = ', alphabets )
numbers.discard(e)
print('Alphabets = ', alphabets )
輸出:
Alphabets = {'a', 'b', 'd'}
Alphabets = {'a', 'b', 'd'}
示例 Python 中丟棄()的工作方式
alphabets = {'a', 'b', 'c', 'd'}
#Returns none
print(alphabets .discard(c))
print('Alphabets = ', alphabets )
輸出:
None
Alphabets = {'a', 'b', 'd'} 文章名稱:創(chuàng)新互聯(lián)Python教程:Pythondiscard()
文章起源:http://m.fisionsoft.com.cn/article/cdhoded.html


咨詢
建站咨詢
