python 中的discard()函數有助於在元素存在的情況下從集合中移除或丟棄指定的元素。
**s.discard(element)** #where element may be a integer,string etc.
丟棄()參數:
discard()函數接受一個參數。此方法類似於remove()方法,不同之處在於,如果給定元素不在集合中,discard()方法會引發錯誤,但remove()不會。
| 參數 | 描述 | 必需/可選 |
|---|---|---|
| 元素 | 要搜索和移除的項目 | 需要 |
丟棄()返回值
discard()方法不返回任何內容。它只是通過從集合中移除指定的元素來更新集合。
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'} 原創文章,作者:REAGJ,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/129103.html
微信掃一掃
支付寶掃一掃