博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 示例_带有示例的Python Set remove()方法
阅读量:2537 次
发布时间:2019-05-11

本文共 2085 字,大约阅读时间需要 6 分钟。

python 示例

设置remove()方法 (Set remove() Method)

remove() method is used to remove a specified element from the set, the method accepts an element and removes from the set if it exists, if the element does not exist in the set, the method returns an error.

remove()方法用于从集合中删除指定的元素,该方法接受一个元素并从集合中删除(如果存在),如果该元素在集合中不存在,则该方法返回错误。

Syntax:

句法:

set_name.remove(element)

Parameter(s):

参数:

  • element – It represents the element 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 remove() Method with Example# declaring the setscars = {
"Porsche", "Audi", "Lexus", "Mazda", "Lincoln"}nums = {
100, 200, 300, 400, 500}# printing the sets before removingprint("Before the calling remove() method...")print("cars: ", cars)print("nums: ", nums)# Removing the elements from the setscars.remove("Porsche")cars.remove("Mazda")nums.remove(100)nums.remove(500)# printing the sets after removingprint("After the calling remove() method...")print("cars: ", cars)print("nums: ", nums)

Output

输出量

Before the calling remove() method...cars:  {'Mazda', 'Audi', 'Porsche', 'Lexus', 'Lincoln'}nums:  {100, 200, 300, 400, 500}After the calling remove() method...cars:  {'Audi', 'Lexus', 'Lincoln'}nums:  {200, 300, 400}

Demonstrating example, how method raises error if element is not find in the set?

演示示例,如果在集合中找不到元素,方法将如何引发错误?

Example 2:

范例2:

# Python Set remove() Method with Example# declaring the setscars = {
"Porsche", "Audi", "Lexus", "Mazda", "Lincoln"}# printing the setprint("cars: ", cars)# removing an item (which exists in the set)cars.remove("Porsche")# printing the set againprint("cars: ", cars)# removing an item (which does not exist in the set)# it will return an errorcars.remove("BMW")

Output

输出量

cars:  {'Mazda', 'Audi', 'Lexus', 'Porsche', 'Lincoln'}cars:  {'Mazda', 'Audi', 'Lexus', 'Lincoln'}Traceback (most recent call last):  File "main.py", line 15, in 
cars.remove("BMW")KeyError: 'BMW'

翻译自:

python 示例

转载地址:http://xgazd.baihongyu.com/

你可能感兴趣的文章
旧的起点(开园说明)
查看>>
生产订单“生产线别”带入生产入库单
查看>>
crontab导致磁盘空间满问题的解决
查看>>
java基础 第十一章(多态、抽象类、接口、包装类、String)
查看>>
Hadoop 服务器配置的副本数量 管不了客户端
查看>>
欧建新之死
查看>>
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
mysql 日期时间运算函数(转)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>