方法 后 throws
设置throw()方法 (Set discard() Method)
discard() method is used to remove a given element from the set, it accepts an element and removes it from the set.
reject()方法用于从集合中移除给定元素,它接受一个元素并将其从集合中移除。
Note: If the given element does not exist in the set, the “discard() method” does not return any error.
注意 :如果给定元素在集合中不存在,则“ discard()方法”不会返回任何错误。
Syntax:
句法:
set_name.discard(element)
Parameter(s):
参数:
element – It represents the element/value to be removed from the list.
element –它表示要从列表中删除的元素/值。
Return value:
返回值:
The return type of this method is <class 'NoneType'>, it returns nothing.
此方法的返回类型为<class'NoneType'> ,它什么也不返回。
Example 1:
范例1:
# Python Set discard() Method with Example
# declaring the sets
cars_1 = {"Porsche", "Audi", "Lexus"}
cars_2 = {"Porsche", "Mazda", "Lincoln"}
# printing the sets before discard() call
print("cars_1:", cars_1)
print("cars_2:", cars_2)
# removing an element from cars_1
cars_1.discard("Porsche")
# removing an element from cars_2
cars_2.discard("Lincoln")
# printing the sets after dLincoln() call
print("cars_1:", cars_1)
print("cars_2:", cars_2)
Output
输出量
cars_1: {'Audi', 'Lexus', 'Porsche'}
cars_2: {'Mazda', 'Porsche', 'Lincoln'}
cars_1: {'Audi', 'Lexus'}
cars_2: {'Mazda', 'Porsche'}
Example 2:
范例2:
# Python Set discard() Method with Example
# declaring a set
cities = {"New Delhi", "Banglore", "Indore", "Gwalior"}
# printing set before discard() call
print("cities:", cities)
# removing "New Delhi" from the set
cities.discard("New Delhi")
# removing an element that does not exist
# in the set, thus we will remove "Mumbai"
# method discard() will not give any error
cities.discard("Mumbai")
# printing set after discard() call
print("cities:", cities)
Output
输出量
cities: {'New Delhi', 'Gwalior', 'Indore', 'Banglore'}
cities: {'Gwalior', 'Indore', 'Banglore'}
翻译自: https://www.includehelp.com/python/set-discard-method-with-example.aspx
方法 后 throws