列表的使用
列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作
定义列表
1 >>> names = ["Zhangsan","Lisi","Wangwu","Zhaoliu"]
通过下标访问列表中的元素,下标从0开始计数
1 >>> names[0] 2 'Zhangsan' 3 >>> names[2] 4 'Wangwu' 5 >>> names[-1] #-1表示最后一位 6 'Zhaoliu' 7 >>> names[-2] 8 'Wangwu'
切片:
1 >>> names = ["Zhangsan","Lisi","Wangwu","Zhaoliu","Qianqi","Sunba"] 2 >>> names[1:4] #取下标1至下标4之间的数字,包括1,不包括4(顾头不顾尾) 3 ['Lisi', 'Wangwu', 'Zhaoliu'] 4 >>> names[1:-1] #取下标1至-1的值,不包括-1 5 ['Lisi', 'Wangwu', 'Zhaoliu', 'Qianqi'] 6 >>> names[0:3] 7 ['Zhangsan', 'Lisi', 'Wangwu'] 8 >>> names[:3] #如果是从头开始取,0可以忽略,跟上句效果一样 9 ['Zhangsan', 'Lisi', 'Wangwu'] 10 >>> names[3:] #如果想取最后一个,必须不能写-1,只能这么写 11 ['Zhaoliu', 'Qianqi', 'Sunba'] 12 >>> names[3:-1] #这样-1就不会被包含了 13 ['Zhaoliu', 'Qianqi'] 14 >>> names[0::2] #后面的2是代表,每隔一个元素,就取一个 15 ['Zhangsan', 'Wangwu', 'Qianqi'] 16 >>> names[::2] #和上句效果一样 17 ['Zhangsan', 'Wangwu', 'Qianqi']
追加:append
1 >>> names = ["Zhangsan","Lisi","Wangwu","Zhaoliu","Qianqi","Sunba"] 2 >>> names.append("我是新来的") 3 >>> names 4 ['Zhangsan', 'Lisi', 'Wangwu', 'Zhaoliu', 'Qianqi', 'Sunba', '我是新来的']
插入:insert
1 >>> names 2 ['Zhangsan', 'Lisi', 'Wangwu', 'Zhaoliu', 'Qianqi', 'Sunba', '我是新来的'] 3 >>> 4 >>> names.insert(2,"从Wangwu前面插入") 5 >>> names 6 ['Zhangsan', 'Lisi', '从Wangwu前面插入', 'Wangwu', 'Zhaoliu', 'Qianqi', 'Sunba', '我是新来的'] 7 >>> 8 >>> names.insert(5,"插入到Wangwu后面") 9 >>> names 10 ['Zhangsan', 'Lisi', '从Wangwu前面插入', 'Wangwu', 'Zhaoliu', '插入到Wangwu后面', 'Qianqi', 'Sunba', '我是新来的']
修改:
1 >>> names 2 ['Zhangsan', 'Lisi', '从Wangwu前面插入', 'Wangwu', 'Zhaoliu', '插入到Wangwu后面', 'Qianqi', 'Sunba', '我是新来的'] 3 >>> names[2] = "替补上场" 4 >>> names 5 ['Zhangsan', 'Lisi', '替补上场', 'Wangwu', 'Zhaoliu', '插入到Wangwu后面', 'Qianqi', 'Sunba', '我是新来的']
删除:del
1 >>> del names[2] #删除指定下标 2 >>> names 3 ['Zhangsan', 'Lisi', 'Wangwu', 'Zhaoliu', '插入到Wangwu后面', 'Qianqi', 'Sunba', '我是新来的'] 4 >>> del names[4] 5 >>> names.remove("Wangwu") #删除指定列表元素 6 >>> names 7 ['Zhangsan', 'Lisi', 'Zhaoliu', 'Qianqi', 'Sunba', '我是新来的'] 8 >>> names.pop() #默认删除最后一位,添加下标后效果等同del names[x] 9 '我是新来的' 10 >>> names 11 ['Zhangsan', 'Lisi', 'Zhaoliu', 'Qianqi', 'Sunba']
扩展:extend
1 >>> names 2 ['Zhangsan', 'Lisi', 'Zhaoliu', 'Qianqi', 'Sunba'] 3 >>> names2 = [1,2,3,4] 4 >>> names.extend(names2) 5 >>> names 6 ['Zhangsan', 'Lisi', 'Zhaoliu', 'Qianqi', 'Sunba', 1, 2, 3, 4]
统计:count
1 >>> names = ["Zhangsan","Lisi"
,"Wangwu","Zhaoliu","Qianqi","Sunba","Wangwu"] 2 >>> names.count("Wangwu") 3 2
排序:sort
1 >>> names = ["!Zhangsan","123Lisi","$Wangwu","zhaoliu","Qianqi","Sunba"] 2 >>> names.sort() 3 >>> names 4 ['!Zhangsan', '$Wangwu', '123Lisi', 'Qianqi', 'Sunba', 'zhaoliu'] #按ASC码顺序排列
反转:reserse
1 >>> names 2 ['!Zhangsan', '$Wangwu', '123Lisi', 'Qianqi', 'Sunba', 'zhaoliu'] 3 >>> names.reverse() 4 >>> names 5 ['zhaoliu', 'Sunba', 'Qianqi', '123Lisi', '$Wangwu', '!Zhangsan']
获取下标:index
1 >>> names 2 ['zhaoliu', 'Sunba', 'Qianqi', '123Lisi', '$Wangwu', '!Zhangsan'] 3 >>> 4 >>> names.index("Qianqi") 5 2
清空: clear
1 >>> names 2 ['zhaoliu', 'Sunba', 'Qianqi', '123Lisi', '$Wangwu', '!Zhangsan'] 3 >>> names.clear() 4 >>> names 5 []