/usr/bin/pythonlist1=['physics','chemistry',1997,2000]printlist1dellist1[2]print"After deleting value at index 2 :"printlist1 以上实例输出结果: ['physics','chemistry',1997,2000]Afterdeleting value at index2:['physics',
需要注意,remove方法没有返回值,而且如果删除的元素不在列表中的话,会发生报错。 >>>lst=[1,2,3]>>>lst.remove(4)Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>ValueError:list.remove(x):xnotinlist pop L.pop([index]) -> item -- remove and return item at index (default la...
File"<stdin>", line1,in<module> ValueError:list.remove(x): xnotinlist pop L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range. pop是删除指定索引位置的元素,参数是 index。如果不指定索引,默认删除列表...
4、list.index(obj):从列表中找出某个值第一个匹配项的索引位置 5、list.insert(index, obj):将对象插入列表 6、list.pop(obj=list[-1]):移除列表中的一个元素(默认最后一个元素),并且返回该元素的值 7、list.remove(obj):移除列表中某个值的第一个匹配项 8、list.reverse():反向列表中元素 9、list....
L.pop(index) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range. pop是删除指定索引位置的元素,参数是 index。如果不指定索引,默认删除列表最后一个元素。 代码语言:python ...
在python中删除原list中的某个元素有多种方法,下面介绍四种。 1.remove(value) 函数:(参数是值) 源码中解释如下: L.remove(value) -> None -- remove first occurrence of value. Raises ValueError if the value is not present. 例子: # 1.remove()函数: ...
L.pop([index]) -> item – remove and return item at index (default last). Raises IndexError if list is empty or index is out of range. pop是删除指定索引位置的元素,参数是 index。如果不指定索引,默认删除列表最后一个元素。 >>>lst=[1,2,3]>>>lst.pop(1)2>>>lst[1,3]>>>lst=[1...
del list[index]:从给定的切片或索引中删除项目。被删除的对象不会被返回。当你根据位置删除切片或项目...
2 :2001四、删除列表元素可以使用 del 语句来删除列表的的元素,如下实例:list1 = ['physics', 'chemistry', 1997, 2000];print list1;del list1[2];print "After deleting value at index 2 : "print list1;以上实例输出结果:['physics', 'chemistry', 1997, 2000]After deleting value at index 2...
remove(1) print(lst) # 删除一个不存在的值时,会抛出ValueError异常 # lst.remove(10)# pop # 默认返回并删除最后一个元素 lst.pop() print(lst) # pop可以有参数 # 返回并删除索引所在位置的元素 lst.pop(1) print(lst) # 当pop不存在的索引时,抛出IndexError异常 # lst.pop(100) 运行结果为:...