开发环境: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)
结果图片: