方法 后 throws_带有示例的python设置throws()方法

方法 后 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值