python列表list函数_python list列表常用系统函数

开发环境:vs 2019append()向列表中追加数据

insert()向指定的索引处插入数据

remove()移除值为1的从开始处找到的第一个元素

pop()移除指定索引位置的值

index()返回指定值的索引位置

count()获取指定值在列表中出现的次数

sort()排序

reverse()反转数据

clear()清空列表

copy()拷贝列表

下面为写的测试代码:hello = ["hello", 2, "how", 1, 3, "you"]

print ("hello =", hello)

print ("------------------------------append用法")

hello.append(["nice", 1, "a"]) #append用法

print ("hello =", hello)

hello.append("are"); #append用法

print ("hello =", hello)

print ("------------------------------insert用法")

hello.insert(3, "are") #insert用法,在第三个索引下插入,第三个索引是1,因此在1处插入,把1向后移动1位

print ("hello =", hello)

print ("------------------------------remove用法")

hello.remove(1) #remove移除值为1的从开始处找到的第一个元素

print ("hello =", hello)

print ("------------------------------pop用法")

hello.pop(6); #pop移除指定索引位置的值

print ("hello =", hello)

print ("------------------------------index用法")

print ("hello =", hello.index("how")) #index返回指定值的索引位置

#print (hello.index("z")) z不存在列表里面,因此报错,因此查询的时候一定要,查询在列表的

print ("------------------------------count用法")

print ("hello =", hello.count("are")) #count 获取指定值在列表中出现的次数

print ("hello =", hello.count("how"))

print ("------------------------------sort用法")

#print (hello.sort()) 此处报错因为,我们的列表中有字符串,有数字,就没法比较了,需要是纯数字或纯字符才行

hi = [1, 3, 2, 5, 8]

hi.sort() #sort进行排序

print ("hi =", hi)

hiStr = ["hello", "how", "are", "you" ]

hiStr.sort() #sort进行排序

print ("hiStr =", hiStr)

print ("------------------------------reverse用法")

hiStr.reverse() #reverse 反转数据,好比列是1,3,7反转就是反过来7,3,1

print ("hiStr =", hiStr)

print ("------------------------------clear用法")

print ("hi =", hi)

hi.clear();

print ("hi =", hi) #clear 清空数据使用

print ("------------------------------copy用法")

hiCopy = hiStr.copy()

#copy 只是把当前列表,复制给别一个列表hiCopy,

#但是改变hiCopy列表值,并不影响hiStr的值,

#这里可以确定两个列表引用的不是一块内存地址

print ("hiCopy =" ,hiCopy)

hiCopy[0]="python"

print ("hiCopy =", hiCopy)

print ("hiStr =", hiStr)

结果图片:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值