python中的列表和其它语言不一样的地方在于:python中的数组元素类型可以是任意类型,以下只记录一些要点:
1.追加
member = [1,2,"hello world"]
member.append("3")
结果:
member = [1,2,"hello world",3]
这种只能插入一个元素,在列表末尾追加
2.扩展
member = [1,2,"hello world"]
member.extend([3,4,5,6])
结果:
member = [1,2,"hello world",3,4,5,6]
这种适合整合两个列表,相当于多个追加
3.插入
member = [1,2,"hello world"]
member.insert(1,"3")
结果:
member = [1,3,2,"hello world"]
4.弹出
python的列表是用栈实现的,所以有pop()方法
member = [1,2,"hello world"]
member.pop()
结果:
"hello world"
5.分片(slice)
member = [1,2,"hello world"]
member[0:1]
结果:
[1]
也就是说,从下标0开始,数一位元素,生成列表
特殊(偷懒)情况
member = [1,2,"hello world"]
member[:3]
结果:
[1,2,"hello world"]
默认从0开始,往后数3位
member = [1,2,"hello world"]
member[1:]
结果:
[2,"hello world"]
从指定下标开始,到结束
member = [1,2,"hello world"]
member[:]
结果:
[1,2,"hello world"]
返回原列表