一、列表
列表是一个可以包含所以数据类型的对象的位置有序集合,它是可以改变的。
1、列表的序列操作(Python3)
- >>> one_list = [1,2,3,4]
- >>> two_list = ["jonny","jery","david"]
- #统计元素数量
- >>> len(one_list)
- 4
- #索引,根据偏移量,左起从0开始,右起从-1开始
- >>> one_list[0]
- 1
- >>> one_list[-1]
- 4
- #切片
- >>> one_list[0:2]
- [1, 2]
- >>> one_list[:-1]
- [1, 2, 3]
- >>> one_list[1:]
- [2, 3, 4]
- #步进,默认为1
- >>> one_list[::2]
- [1, 3]
- #扩展进来新的列表
- >>> new_list = one_list + two_list
- >>> print(new_list)
- [1, 2, 3, 4, 'jonny', 'jery', 'david']
2、列表的方法
- #在列表中加入元素
- >>> one_list.append("join")
- >>> print(one_list)
- [1, 2, 3, 4, 'join']
- #查看元素在列表中的偏移量
- >>> one_list.index('join')
- 4
- #统计元素在列表中出现的次数
- >>> one_list = [1,2,3,4,"join","jonny",3,4,2,45,32,"gg",45]
- >>> one_list.count(3)
- 2
- #在列表中指定位置插入元素
- >>> one_list.insert(1,"insert_ele")
- >>> print(one_list)
- [1, 'insert_ele', 2, 3, 4, 'join', 'jonny', 3, 4, 2, 45, 32, 'gg', 45]
- #移除指定元素
- >>> one_list.remove("insert_ele")
- >>> print(one_list)
- [1, 2, 3, 4, 'join', 'jonny', 3, 4, 2, 45, 32, 'gg', 45]
- #通过附加来自可迭代的元素扩展列表(字符串,列表,元组等)
- >>> one_list.extend("extend")
- >>> print(one_list)
- [1, 2, 3, 4, 'join', 'jonny', 3, 4, 2, 45, 32, 'gg', 45, 'e', 'x', 't', 'e', 'n', 'd']
- #移除指定偏移量的元素,不指定则为随机移除
- >>> one_list = [1,2,3,4]
- >>> one_list.pop()
- 4
- >>> one_list.pop(1)
- 2
- >>> print(one_list)
- [1, 3]
- #根据ASCII码排序,python2.X系列可对所有元素排序,3.X系列只能对相同类型元素排序
- Python3.6
- >>> one_list = [3,6,2,8]
- >>> one_list.sort()
- >>> print(one_list)
- [2, 3, 6, 8]
- Python2.7
- >>> two_list = [3,6,4,7]
- >>> two_list.extend("djttdkx01")
- >>> print two_list
- [3, 6, 4, 7, 'd', 'j', 't', 't', 'd', 'k', 'x', '0', '1']
- >>> two_list.sort()
- >>> print two_list
- [3, 4, 6, 7, '0', '1', 'd', 'd', 'j', 'k', 't', 't', 'x']
- #反转列表
- >>> two_list.reverse()
- >>> print two_list
- ['x', 't', 't', 'k', 'j', 'd', 'd', '1', '0', 7, 6, 4, 3]
- #列表的复制方法一:复制第一级,对于嵌套的列表只是复制其引用位置
- >>> one_list = [1,2,3,4,[5,6,7,8]]
- >>> two_list = one_list[:]
- >>> print(two_list)
- [1, 2, 3, 4, [5, 6, 7, 8]]
- >>> id(one_list)
- 5697352
- >>> id(two_list)
- 50197576
- #列表复制方法二:复制第一级,对于嵌套的列表只是复制其引用位置
- >>> three_list = one_list.copy()
- >>> print(three_list)
- [1, 2, 3, 4, [5, 6, 7, 8]]
- >>> id(three_list)
- 49960008
- #列表复制方法三:copy模块的深度复制
- >>> import copy
- >>> four_list = copy.deepcopy(one_list)
- >>> print(four_list)
- [1, 2, 3, 4, [5, 6, 7, 8]]
- >>> one_list[4][0] = 55
- >>> print(two_list)
- [1, 2, 3, 4, [55, 6, 7, 8]]
- >>> print(three_list)
- [1, 2, 3, 4, [55, 6, 7, 8]]
- >>> print(four_list)
- [1, 2, 3, 4, [5, 6, 7, 8]]
3、列表的嵌套
- >>> one_list = [2,3,1,7,[2,"gg","david"],87,98]
- >>> one_list[4][1][1]
- 'g'
- >>> one_list[4][2]
- 'david'
4、列表解析
- >>> one_list = [[1,2,3],[4,5,6],[7,8,9]]
- >>> new_list = [row[0] for row in one_list]
- >>> print(new_list)
- [1, 4, 7]
- >>> two_list = [row[1] % 2 for row in one_list]
- >>> print(two_list)
- [0, 1, 0]
5、练习
- '''
- 练习:找列表中的9替换成9999
- 同时找出所有的34,全删掉
- '''
- one_list = ["jym","alex",9,"jonny","sun",3,6,7,8,2,3,1,9,34,543,43,32,34,"gg","jids"]
- print(one_list)
- for i in range(one_list.count(9)):
- one_list [one_list.index(9)] = 9999
- for i in range(one_list.count(34)):
- del one_list[one_list.index(34)]
- print(one_list)
二、元组
元组是不可改变的列表,编写在圆括号中,支持任意类型,任意嵌套等常见操作
1、元组的序列操作
- >>> one_tuple = (1,2,3,4)
- #统计元素个数
- >>> len(one_tuple)
- 4
- #元组附加
- >>> two_tuple = one_tuple + (5,6)
- >>> print(two_tuple)
- (1, 2, 3, 4, 5, 6)
- #索引
- >>> one_tuple[0]
- 1
- >>> one_tuple[-2]
- 3
2、元组的方法
- #查看元素在元组中的偏移量
- >>> one_tuple.index(2)
- 1
- #统计元素在元组中出现的次数
- >>> one_tuple.count(2)
- 1
来源: