Python入门之列表
列表
- 列表创建list
>>> lst = ["chengd","python","good"] >>> lst ['chengd', 'python', 'good'] >>> lst1 = [1,2,3,4,5,7] >>> lst1 [1, 2, 3, 4, 5, 7] >>> lst2 = ["chengd",99,[1,"python"],("world","hello",1)] >>> lst2 ['chengd', 99, [1, 'python'], ('world', 'hello', 1)]
注:list元素可以是python各种数据类型,并且用逗号分割
- 通过其他列表创建新列表
#通过其他列表内容正确创建新列表 >>> lst ['chengd', 'python', 'good'] >>> lst1 = lst.copy() >>> lst1 ['chengd', 'python', 'good'] >>> lst2 = list(lst) >>> lst2 ['chengd', 'python', 'good'] >>> lst1.remove("good") >>> lst2.remove("good") >>> lst1 ['chengd', 'python'] >>> lst2 ['chengd', 'python'] >>> lst ['chengd', 'python', 'good'] #错误创建:lst3 = lst #修改lst3时也会修改原列表lst;因为lst3是直接引用lst;如下 >>> lst ['chengd', 'python', 'good'] >>> lst3 = lst >>> lst3 ['chengd', 'python', 'good'] >>> lst3.remove("chengd") >>> lst3 ['python', 'good'] >>> lst ['python', 'good']
- 列表元素获取:与字符串的索引一样,列表索引从0开始。列表可以进行截取、组合等;列表元素下标从前往后规律:0,1,2,3,4......;列表元素下标从后往前规律:-1,-2,-3,-4......
>>> lst [1, 2, 3, 4, 2, 3, 2, 2, 5, 6] >>> lst[0] 1 >>> lst[-1] 6 >>> lst[-2] 5 >>> lst[1] 2 >>> lst[0:3] [1, 2, 3] #截取元素时,是指截取到第几个元素而非是截取到的下标;lst[0:3]表示截取到第3个元素即下标0-2的元素
- 列表常用python内置函数
- len(list):统计列表元素个数
>>> lst [5, 4, 3, 3, 2, 2, 1] >>> lst1 ['chengd', 'good', 'python'] >>> len(lst) 7 >>> len(lst1) 3
- max(list):返回列表元素最大值
>>> lst [5, 4, 3, 3, 2, 2, 1] >>> max(lst) 5 >>> lst1 ['chengd', 'good', 'python'] >>> max(lst1) 'python'
- min(list):返回列表元素最小值
>>> lst [5, 4, 3, 3, 2, 2, 1] >>> min(lst) 1
- list(seq):将序列seq转换为列表
>>> list(i for i in range(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> lst = list(i for i in range(10)) >>> lst [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
- 列表常用方法:
-
list.append("obj"):在列表末尾添加新的元素obj;obj可为字符串,列表,元组,直接作为一个整体加入list
>>> lst ['python', 'good'] >>> lst.append("lucky") >>> lst ['python', 'good', 'lucky'] >>> lst.append(["chengd",999]) >>> lst ['python', 'good', 'lucky', ['chengd', 999]]
- list.extend(obj):在列表末尾一次性追加另一个序列(列表,元组)中的多个值(用新列表扩展原来的列表)
>>> lst ['python', 'good', 'lucky'] >>> lst.extend(["chengd",999]) >>> lst ['python', 'good', 'lucky', 'chengd', 999] >>> lst.extend(("add","wx")) >>> lst ['python', 'good', 'lucky', 'chengd', 999, 'add', 'wx']
- list.clear():清空列表
>>> lst ['python', 'good', 'lucky', 'chengd', 999, 'add', 'wx'] >>> lst.clear() >>> lst []
- list.count(obj):统计元素obj出现的次数
>>> lst = [1,2,3,4,2,3,2,2,5,6] >>> lst [1, 2, 3, 4, 2, 3, 2, 2, 5, 6] >>> lst.count(2) 4
- list.index('obj'[,start,end]):从列表中找出某个值第一个匹配项的索引位置;可以指定起始和结束位置
>>> lst [1, 2, 3, 4, 2, 3, 2, 2, 5, 6] >>> lst.index(2) 1 >>> lst.index(3) 2
- list.insert(index,obj):指定索引下标位置插入元素obj
>>> lst [1, 2, 3, 4, 2, 3, 2, 2, 5, 6] >>> lst.insert(2,"chengd") >>> lst [1, 2, 'chengd', 3, 4, 2, 3, 2, 2, 5, 6]
- list.pop(index):删除并弹出索引为index的元素;默认不指index删除弹出最后一个元素
>>> lst [1, 2, 'chengd', 3, 4, 2, 3, 2, 2, 5, 6] >>> lst.pop(1) 2 >>> lst [1, 'chengd', 3, 4, 2, 3, 2, 2, 5, 6] >>> lst.pop() 6 >>> lst [1, 'chengd', 3, 4, 2, 3, 2, 2, 5]
- list.remove(obj):指定删除obj元素,列表中有多个obj元素则只会删除第一个
>>> lst [1, 'chengd', 3, 4, 2, 3, 2, 2, 5] >>> lst.remove(2) >>> lst [1, 'chengd', 3, 4, 3, 2, 2, 5] #1, 'chengd', 3, 4, 2(被删除), 3, 2, 2, 5 #1, 'chengd', 3, 4, 3, 2, 2, 5
- list.reverse():反转列表元素顺序
>>> lst [1, 'chengd', 3, 4, 3, 2, 2, 5] >>> lst.reverse() >>> lst [5, 2, 2, 3, 4, 3, 'chengd', 1]
- list.sort():列表元素从小到大排序;注意列表元素类型要一致
>>> lst [2, 2, 3, 3, 4, 5, 1] >>> lst.sort() >>> lst [1, 2, 2, 3, 3, 4, 5] >>> lst1 = ["chengd","python","good"] >>> lst1 ['chengd', 'python', 'good'] >>> lst1.sort() >>> lst1 ['chengd', 'good', 'python'] #列表从大到小排序 >>> lst [2, 2, 3, 3, 4, 5, 1] >>> lst.sort() >>> lst [1, 2, 2, 3, 3, 4, 5] >>> lst.reverse() >>> lst [5, 4, 3, 3, 2, 2, 1]

浙公网安备 33010602011771号