首页 / 知识

python中如何清空数组(列表)元素

2023-11-12 13:25:00

python本身并没有数组类型,但是他的Numpy库中有数组类型。通常情况下,python中的数组指的是列表。

第一种:

代码示例:

List=[5,6,7]

print('List清空前:',List)

#重新初始化列表

List=[]

print('List清空后:',List)

第二种:

使用clear()方法

clear()方法是用于清空列表的,基本语法:

list.clear()

代码示例:

List=[6,0,4,1]

print('List清空前:',List)

#清空元素

List.clear()

print('List清空后:',List)

第三种:

使用dela[:]

del可以用来清除范围中的列表元素,如果我们不给出范围,则删除所有元素,做到清空列表。

代码示例

List1=[8,6,16]

List2=[4,5,7]

print("List1清空前:"+str(List1))

#使用del删除List1

delList1[:]

print("List1清空后:"+str(List1))

print('\n')

print("List2清空前:"+str(List2))

#使用del删除List2

delList2[:]

print("List2清空后:"+str(List2))

以上内容为大家介绍了python中如何清空数组(列表)元素,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注我们

培训代码数组元素列表

最新内容

相关内容

热门文章

推荐文章

标签云

猜你喜欢