1.用下标取得某个值,元素从序号0开始
2.负数下标:从最后一个值开始(-1)
>>> [1,2,3] [1, 2, 3] >>> sp=['a','b','c'] >>> sp[0] 'a' >>> sp[1] 'b' >>> ['a','b','c'][1] 'b' >>> 'kk ' + sp[1] 'kk b' >>> sp=[['a','b','c'],[1,2,3,4]] >>> sp[0] ['a', 'b', 'c'] >>> sp[0][1] 'b' >>> sp[2][2] Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> sp[2][2] IndexError: list index out of range >>> sp[1][2] 3 >>> >>> sp=['a','b','c'] >>> sp[-2] 'b' >>> sp[-1] 'c'
3.切片,sp[1:3]代表第三个元素以前的两个元素(3-1==2)从第1个元素开始,到第二个数,不包括第3个。
(从第0个开始计数).len函数求列表长度。
>>> sp[1:2] #切片 ['b'] >>> sp[1:3] ['b', 'c'] >>> sp=['a','b','c','d','e'] >>> sp[0:4] ['a', 'b', 'c', 'd'] >>> sp[1:] ['b', 'c', 'd', 'e'] >>> sp[:2] ['a', 'b'] >>> len(sp) 5
4.用下标改参数的值(相当于赋值)
>>> sp[1]=1 >>> sp ['a', 1, 'c', 'd', 'e'] >>> sp[-1]=88 >>> sp ['a', 1, 'c', 'd', 88] >>>
5.列表连接与复制,删除(del)
>>> [1,2,3]+['a','b','c'] [1, 2, 3, 'a', 'b', 'c'] >>> [1,2,3]*2 [1, 2, 3, 1, 2, 3] >>> sp=[1,2] >>> sp=sp+['7','8','9'] >>> sp [1, 2, '7', '8', '9'] >>> del sp[0] >>> sp [2, '7', '8', '9'] >>> del sp[3] >>> sp [2, '7', '8']
6.for循环与in,not in用法
>>> >>> 1 in [1,2,3] True >>> sp=['a','b','c'] >>> 'd' in sp False >>> 'b' in sp True for i in range(4) : print(i) ''' ''' #end is 0 1 2 3 ''' #end for i in [1,2,3,4,5] : print(i) #end is 1,2,3,4,5
7.增强赋值 (变量数目与长度必须相等)
>>> ca=['a','b','c'] >>> si,so,sp = ca >>> si 'a' >>> so 'b' >>> sp 'c' >>> sp=5 >>> sp=sp+1 >>> sp 6 >>> sp +=1 >>> sp 7
2.1方法:用index在列表查找值
找不到会报错;有多个值默认报第一次出现的下标
>>> sp=[6,7,8,9] >>> sp.index(8) 2 >>> sp.index(0) Traceback (most recent call last): File "<pyshell#72>", line 1, in <module> sp.index(0) ValueError: 0 is not in list >>> sp.index(6) 0 >>> sp=[6,7,8,9,6] >>> sp.index(6) 0
2.2 用append(默认在末尾添加)和insert(任意位置添加)为列表添加值,
remove删除元素,多次出现只删除第一次出现的
>>> >>> sp.append(10) >>> sp [6, 7, 8, 9, 6, 10] >>> sp.insert(1,'a') >>> sp [6, 'a', 7, 8, 9, 6, 10] >>> sp.remove(7) >>> sp [6, 'a', 8, 9, 6, 10] >>> sp.remove(6) >>> sp ['a', 8, 9, 6, 10]
2.3 用sort方法排序。(升序:列表名+.sort()或.降序:sort(reverse=True))仅支持同类元素排序
sp=[-5,-1,0,5,8] >>> sp.sort() >>> sp [-5, -1, 0, 5, 8] p.sort(reverse=True) >>> sp [8, 5, 0, -1, -5]
2.4 字符串内容不可被改变;列表内容可更改;元组的参数不可更改删除eg:(1,3,5)
元组格式: ( )
list可将元组转列表
tuple可将列表转元组
>>> nn='Asd' >>> nn='lv' >>> nn[1]=k Traceback (most recent call last): File "<pyshell#99>", line 1, in <module> nn[1]=k NameError: name 'k' is not defined kk=(1,2,3) >>> kk[1] 2 >>> >>> list('abcdef') ['a', 'b', 'c', 'd', 'e', 'f'] >>> >>> tuple(['a','b','c']) ('a', 'b', 'c') >>> list(('cat','dog','n')) ['cat', 'dog', 'n']
变量引用
#两变量指向的同一个列表,改变一个时,俩参数实际值都改变
sp=42 tt=sp sp=20 print(sp) print(tt) """ """ #end is 20 42 """ """ #两变量指向的同一个列表,改变一个时,俩参数实际值都改变 sp=[1,3,5,6] #赋值列表给变量 tt=sp #吧 sp 复制到tt变量 sp[0]='lindar' print(tt) print(sp) """ """ end is ['lindar', 3, 5, 6] ['lindar', 3, 5, 6] """
附录
""" #列表元素可随意更改 print('66666666') #打印字符串 print(6+2) #打印结果 eggs=[1,2,'lindar',3,4] #创建列表 print (eggs) #打印列表 eggs[1]=22 #改变列表第1个元素值,从0开始计数 print (eggs) eggs[1]='l' print (eggs) eggs[2]=2 #改变列表第2个元素值,从0开始计数 print (eggs) """ """ 66666666 8 [1, 2, 'lindar', 3, 4] [1, 22, 'lindar', 3, 4] [1, 'l', 'lindar', 3, 4] [1, 'l', 2, 3, 4] """ """ 字符串不能被随意更改 name='hello dear lindar' print(name) #name[1]=tt #erro occured !!! str can not be changed 字符串不能被随意更改 print(name[1]) #print e print(name[0:6]+'k') #end is hello k. name[0:6]代表取第0到5个字符,共6个也就是hello加空格 print(len(name)) new=name[0:6] +'my'+name[10:17] print(new) """ """ hello dear lindar e hello k 17 hello my lindar """ """ #正向,逆向输出 sp=[1,6,2,0] sp.sort() print(sp) # [0, 1, 2, 6] sp.sort(reverse=True) print(sp) # [6, 2, 1, 0] """ """ #元组中的参数值不可更改 sp=[1,6,8] #list(列表) 与tuple(元组)互转,不能对元组变量名更改,只能对相应元素改动 。 print(tuple(['hello'])) #('hello',) print(list(('hello',))) #['hello'] print(list(sp)) #[1, 6, 8] ,没变 """ """ ''' def string(lists): strings = '' for index in range(len(lists)): if index == len(lists) - 2: strings += lists[index] + ', and ' #倒数第一个元素前加,and elif index < len(lists) - 2: #加逗号 strings += lists[index] + ', ' else: strings += lists[index] #链接字符 return strings sp=['a','b','c','d'] sp=string(sp) print(sp) # end is a, b, c, and d ''' """ grid = [['.', '.', '.', '.', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['O', 'O', 'O', 'O', 'O', '.'], ['.', 'O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O', '.'], ['O', 'O', 'O', 'O', '.', '.'], ['.', 'O', 'O', '.', '.', '.'], ['.', '.', '.', '.', '.', '.']] for i in range(len(grid[1])): #横向长度 for j in range(len(grid)): #先向下增加,再向右增加 if j < len(grid) - 1: print(grid[j][i] , end='') #不换行 else: print(grid[j][i]) #换行输出 """ """ ..OO.OO.. .OOOOOOO. .OOOOOOO. ..OOOOO.. ...OOO... ....O.... """ print('666')