python如何对数组删除元素
python中关于对列表元素的删除操作,有以下几种方式
1.remove:删除单个元素,删除首个符合条件的元素,按值删除
举例说明:
>>>str=[1,2,3,4,5,2,6]
>>>str.remove(2)
>>>str
输出
[1,3,4,5,2,6]
2.pop:删除单个或多个元素,按位删除(根据索引删除)
>>>str=[0,1,2,3,4,5,6]
>>>str.pop(1)#pop删除时会返回被删除的元素
>>>str
输出
[0,2,3,4,5,6]
>>>str2=['abc','bcd','dce']
>>>str2.pop(2)
'dce'
>>>str2
['abc','bcd']
3.del:它是根据索引(元素所在位置)来删除
举例说明:
>>>str=[1,2,3,4,5,2,6]
>>>delstr[1]
>>>str
输出
[1,3,4,5,2,6]
补充:删除元素的变相方法
s1=(1,2,3,4,5,6)
s2=(2,3,5)
s3=[]
foriins1:
ifinotins2:
s3.append(i)
print('s1_1:',s1)
s1=s3
print('s2:',s2)
print('s3:',s3)
print('s1_2:',s1)
以上内容为大家介绍了python培训之如何对数组删除元素,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
pythonpython对大小写敏感吗?
python对大小写是敏感的,也就是说,python中是区分大小写的。Python的变量名是区分大小写的,例如:name和Name就是两个变量名,而非相同变量。...详情>>
2023-11-09 23:57:19
python单引号怎么打
实际上在Python中'...'和"..."是完全一样的,但不能出现'..."和"...'这种情况。而将其混合使用会有很多意想不到的效果:具体规...详情>>
2023-11-09 22:41:18
pythonpop什么意思
python中的pop:pop()函数的作用就是用来移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。语法pop()方法语法:list.pop(obj=list...详情>>
2023-11-09 20:54:42
python怎么循环输入
python中的循环语句,可使用for循环,或者while循环。两者的区别是,for循环要求提前知道要循环的次数,而while循环只要满足条件就会循环,直到...详情>>
2023-11-09 16:48:15