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

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

python 示例

列出remove()方法 (List remove() Method)

remove() method is used to remove the first occurrence of the given element, the method is called with this list (the list from which we have to remove the element) and accepts the element to be removed as an argument.

remove()方法用于删除给定元素的第一次出现,该方法用此列表(必须从中删除元素的列表)调用,并接受要删除的元素作为参数。

Syntax:

句法:

list_name.remove(element)

Parameter(s):

参数:

  • element – It represents the element to be removed.

    element –它表示要删除的元素。

Return value:

返回值:

The return type of this method is <class 'NoneType'>, it returns nothing.

此方法的返回类型为<class'NoneType'> ,它什么也不返回。

Example 1:

范例1:

# Python List remove() Method with Example# declaring the listcars = ["BMW", "Porsche", "Audi", "Lexus", "Audi"]# printing the listprint("cars before remove operations...")print("cars: ", cars)# removing "BMW"cars.remove("BMW")# removing "Audi"cars.remove("Audi")# printing the list print("cars after remove operations...")print("cars: ", cars)

Output

输出量

cars before remove operations...cars:  ['BMW', 'Porsche', 'Audi', 'Lexus', 'Audi']cars after remove operations...cars:  ['Porsche', 'Lexus', 'Audi']

Note: If any element doesn't exist in the list, method returns "ValueError".

注意:如果列表中不存在任何元素,则方法返回“ ValueError”。

Example 2:

范例2:

# Python List remove() Method with Example# declaring the listx = [10, 20, 30, 40, 50, 60, 70]# printing the listprint("x before remove operations...")print("x: ", x)x.remove(10)   # will remove 10x.remove(70)   # will remove 70# printing the listprint("x after remove operations...")print("x: ", x)# removing an element that doesn't exist# in the list...x.remove(100) # will generate error

Output

输出量

x before remove operations...x:  [10, 20, 30, 40, 50, 60, 70]x after remove operations...x:  [20, 30, 40, 50, 60]Traceback (most recent call last):  File "main.py", line 19, in 
x.remove(100) # will generate errorValueError: list.remove(x): x not in list

翻译自:

python 示例

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

你可能感兴趣的文章
thymeleaf 自定义标签
查看>>
关于WordCount的作业
查看>>
C6748和音频ADC连接时候的TDM以及I2S格式问题
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>
JAVA 基础 / 第八课:面向对象 / JAVA类的方法与实例方法
查看>>
Ecust OJ
查看>>
P3384 【模板】树链剖分
查看>>
Thrift源码分析(二)-- 协议和编解码
查看>>
考勤系统之计算工作小时数
查看>>
4.1 分解条件式
查看>>
Equivalent Strings
查看>>
flume handler
查看>>
收藏其他博客园主写的代码,学习加自用。先表示感谢!!!
查看>>