一、列表
列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作
定义列表
1 names = ["maqing"," peilin"," xiaoming"," lilei"," hanmeimei"]
通过下标访问列表中的元素,下标从0开始计数
>>> names[0] 'maqing' >>> names[1] 'peilin' >>> names[-1] 'hanmeimei' >>> names[-2] #还可以倒着取 'lilei'
切片:取多个元素
特点:列表取值顾头不顾尾
1 >> names = ["maqing"," peilin"," xiaoming"," lilei"," hanmeimei"] 2 >>> print(names[1:3]) #取下标1~3之间的数字,不包含3 3 [' peilin', ' xiaoming'] 4 >>> print(names[0:3]) #取下标0~3之间的数字,不包含3 5 ['maqing', ' peilin', ' xiaoming'] 6 >>> print(names[:3]) #下标起始位为0时候可以不写 7 >>> print(names[-2:]) #表示从后开始取值,最后两个数字 8 [' lilei', ' hanmeimei']
列表追加
1 #追加使用append,默认在列表中末尾处添加 2 >>> names = ["maqing"," peilin"," xiaoming"," lilei"," hanmeimei"] 3 >>> names.append("我是新添加的!") 4 >>> print(names) 5 ['maqing', ' peilin', ' xiaoming', ' lilei', ' hanmeimei', '我是新添加的!']
插入
1 #插入使用insert,结构为位置+内容 2 ['maqing', ' peilin', ' xiaoming', ' lilei', ' hanmeimei', '我是新添加的!'] 3 >>> names.insert(1,"cajin") 4 >>> print(names) 5 ['maqing', 'cajin', ' peilin', ' xiaoming', ' lilei', ' hanmeimei', '我是新添加的!']
修改
1 #把列表中“xiaoming”修改成“小明” 2 ['maqing', 'cajin', ' peilin', ' xiaoming', ' lilei', ' hanmeimei', '我是新添加的!'] 3 >>> names[3] = "小明" 4 >>> print(names) 5 ['maqing', 'cajin', ' peilin', '小明', ' lilei', ' hanmeimei', '我是新添加的!']
删除
1 #方法一 :直接del names[位置] 2 ['maqing', 'cajin', ' peilin', '小明', ' lilei', ' hanmeimei', '我是新添加的!'] 3 >>> del names[1] 4 >>> print(names) 5 ['maqing', ' peilin', '小明', ' lilei', ' hanmeimei', '我是新添加的!']
1 #方法二:直接删除列表中的数据 2 >>> names.remove(" lilei") 3 >>> print(names) 4 ['maqing', ' peilin', '小明', ' hanmeimei', '我是新添加的!']
1 #方法三:使用pop,从最后面开始删除 2 ['maqing', ' peilin', '小明', ' hanmeimei', '我是新添加的!'] 3 >>> names.pop() 4 '我是新添加的!' 5 >>> print(names) 6 ['maqing', ' peilin', '小明', ' hanmeimei']
合并
1 #extend合并两个列表 2 ['maqing', ' peilin', '小明', ' hanmeimei'] 3 >>> names1 = [1,2,3] 4 >>> names.extend(names1) 5 >>> print(names) 6 ['maqing', ' peilin', '小明', ' hanmeimei', 1, 2, 3]
拷贝
1 #一般情况下copy 2 ['maqing', ' peilin', '小明', ' hanmeimei', 1, 2, 3] 3 >>> names_copy = names.copy() 4 >>> print(names_copy) 5 ['maqing', ' peilin', '小明', ' hanmeimei', 1, 2, 3]
此处描述的copy是最基本的copy,详情请查看下面链接
http://www.cnblogs.com/bldly1989/p/6530650.html
统计
1 #使用count统计 2 >>> names = ["maqing"," peilin","xiaoming","xiaoming"] 3 >>> names.count("xiaoming") 4 2
排序
1 #列表排序按照 字符》数字》大写》小写 默认排序 2 >>> names = ["maqing","Ppeilin","3xiaoming","#lilei","!hanmeimei"] 3 >>> names.sort() 4 >>> print(names) 5 ['!hanmeimei', '#lilei', '3xiaoming', 'Ppeilin', 'maqing']
反转
1 #使用reverse反转列表顺序 2 ['!hanmeimei', '#lilei', '3xiaoming', 'Ppeilin', 'maqing'] 3 >>> names.reverse() 4 >>> print(names) 5 ['maqing', 'Ppeilin', '3xiaoming', '#lilei', '!hanmeimei']
获取下标
1 ['maqing', 'Ppeilin', '3xiaoming', '#lilei', '#lilei','!hanmeimei'] 2 >>> names.index("Ppeilin") 3 1 4 >>> print(names[names.index("#lilei")]) 5 #lilei #只返回查找到的第一个值
二、元组
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
1 names = ("maqing","peilin") 2 print(names.count("maqing")) 3 print(names.index("maqing"))
只有上面这两种使用方法,count和index
来源:https://www.cnblogs.com/bldly1989/p/6529645.html