items = [ 1 , 2 , 3 , 4 , 5 ]
items = [ 1 ] * 5 #生成[1,1,1,1,1]的列表
列表中的某个元素,可以使用列表名+索引 index 的方式来读取。
colors = [ 'red' , 'green' , 'blue' , 'yellow' , 'white' , 'black' ]
print (colors[ 0 ]) #打印出red
print (colors[ 1 ]) #打印出green
print (colors[ 2 ]) #打印出blue
警告
列表的索引 index 一定是从 0 开始的,这是初学者最容易忘记的点。
例如print(colors[1])
打印出来的是 green,而不是 red。
索引也可以从尾部开始,最后一个元素的索引为 -1,往前一位为 -2,以此类推。
colors = [ 'red' , 'green' , 'blue' , 'yellow' , 'white' , 'black' ]
print (colors[ - 1 ]) #打印出black
print (colors[ - 2 ]) #打印出white
print (colors[ - 3 ]) #打印出yellow
系统自带 len 函数,可以计算出一个列表的长度,如colors = ['red', 'green', 'blue', 'yellow', 'white', 'black']
的长度为 6。
colors = [ 'red' , 'green' , 'blue' , 'yellow' , 'white' , 'black' ]
print ( len (colors)) #输出6
用 for 来帮助我们访问列表,可以说最佳拍档。
用 for 来访问列表也有两种模式,一种是索引遍历,一种是元素遍历。
colors = [ 'red' , 'green' , 'blue' , 'yellow' , 'white' , 'black' ]
print (colors[ 0 ])
print (colors[ 1 ])
print (colors[ 2 ])
print (colors[ 3 ])
print (colors[ 4 ])
print (colors[ 5 ])
colors = [ 'red' , 'green' , 'blue' , 'yellow' , 'white' , 'black' ]
for i in range ( len (colors)):
print (colors[i])
colors = [ 'red' , 'green' , 'blue' , 'yellow' , 'white' , 'black' ]
for i in colors:
print (i)
比较之后,我们可以发现,for 的元素遍历语法最精简,但是元素遍历有个很大局限性,那就是不能获取元素的索引和更新此元素。
colors = [ 'red' , 'green' , 'blue' ]
for i in colors:
i = 'white'
print (colors) #还是输出['red', 'green', 'blue']
colors = [ 'red' , 'green' , 'blue' ]
for i in range ( len (colors)):
colors[i] = 'white'
print (colors) #会输出['white', 'white', 'white']
列表可以通过切片的方式来访问列表中的一部分数据,得到一个子列表。
切片的语法是列表名+[start : end : step]。(有没有觉得跟range 函数 的格式很像!)
切片中 start, end, 和 step 参数有时可以忽略不写。
start 不写时,默认取到最左边 end 不写时,默认取到最右边 step 不写时,默认为 1 错误
切片左闭右开
切片一定要记住左取右不取 的原则,和 range 函数是一致的,这是初学者最容易犯错的地方!
例如nums[1:4]
,只能从第 1 个元素开始,取到第 4 个元素的前面一个元素,也就是第 3 个元素,最终只能取到第 1,2,3 个元素,而不能取到第 4 个元素!
nums = [ 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 ]
print (nums[ 1 : 7 : 2 ]) #从第1个元素开始,取到第6个元素,步长为2,打印出[20,40,60]
print (nums[: 4 ]) #从最左边,取到第3个元素,打印出[10,20,30,40]
print (nums[ 4 :]) #从第4个元素开始,取到最右边,打印出[50,60,70,80,90]
print (nums[ 4 : 1 : - 1 ]) #从第4个元素开始,取到第2个元素,步长为-1,打印出[50, 40, 30]
print (nums[:]) #从最左边,取到最右边,打印出[10, 20, 30, 40, 50, 60, 70, 80, 90]
print (nums[:: - 1 ]) #从最左边,取到最右边,步长为-1,打印出[90,80,70,60,50,40,30,20,10]
nums = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
for i in nums[ 4 :]:
print (i, end = ' ' )
程序会输出 ?
nums = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
for i in nums[ 4 : 1 : - 1 ]:
print (i, end = ' ' )
程序会输出 ?
nums = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]
for i in nums[ 1 : 7 : 2 ]:
print (i, end = ' ' )
程序会输出 ?
colors[0]
可以访问列表中的第 0 个元素,也可以通过这种方式直接更新第 0 个元素,例如:colors[0] = 'pink'
,也可以通过切片批量更新元素,colors[1:6:2] = [1,2,3]
。
colors = [ 'red' , 'green' , 'blue' , 'yellow' , 'white' , 'black' ]
colors[ 0 ] = 'pink'
print (colors[ 0 ]) #打印出pink
colors[ 1 : 6 : 2 ] = [ 1 , 2 , 3 ] #也可以通过切片批量更新元素
print (colors) #打印出['pink', 1, 'blue', 2, 'white', 3]
我们可以增加新的元素到列表尾部,使用 append 方法。要添加到任意位置,可以使用 insert 方法。
colors = [ 'red' , 'green' , 'blue' ]
colors.append( 'pink' )
colors.insert( 0 , 'gray' )
print (colors) #打印出['gray', 'red', 'green', 'blue', 'pink']
我们可以使用 pop 方法,通过索引 index 来删除列表中的元素,也可以使用 remove 方法,删除列表中第一个匹配到的元素。
colors = [ 'red' , 'green' , 'blue' , 'black' ]
colors.pop( 0 )
print (colors) #打印出['green', 'blue', 'black']
colors.pop() #不填参数,默认删除最后一个
print (colors) #打印出['green', 'blue']
colors.remove( 'blue' )
print (colors) #打印出['green']
如果多个元素在一行中输入,可以使用之前提到过的 input().split(),将这一行的元素转为列表。
nums = input ().split() #1 2 3
print (nums) #[1,2,3]
如果多个元素逐行输入,则可以使用多个 input()来设置列表元素。
nums = []
for i in range ( 3 ):
nums.append( input ())
print (nums) #[1,2,3]
列表有一些基本操作符是比较常用的,如下:
操作符 结果 描述 1, 2, 3 + 4, 5, 6 1, 2, 3, 4, 5, 6 列表拼接和合并 'Hi!' * 4'Hi!', 'Hi!', 'Hi!', 'Hi!' 列表批量赋值 3 in 1, 2, 3 True 判断元素是否存在于列表中 4 not in 1, 2, 3 False 判断元素是否不在于列表中
列表推导式可以用于从某个序列中,快速构造出新的序列列表。
range()函数
range()函数生成的就是一个序列
items = [i for i in range ( 5 )] #生成[0,1,2,3,4]的列表
items = [i * i for i in range ( 5 )] #生成[0,1,4,9,16]的列表
在计算机的实际运行中,列表等序列存储的其实是一个内存地址,所有的值是在这个地址中依次存储的内容。
而普通的数字和字符串类型的变量,存储的就是一个普通的值。
下面的示例代码中,将 color1 赋值给 color2 是值引用,修改 color2 的值,不会影响 color1。
而将 colors 赋值给 new_colors 时,是地址引用,是将 colors 的地址赋予给 new_colors,所以修改 new_colors 的值,就是修改 colors 的值。
color1 = 'pink'
color2 = color1
color2 = 'white'
print (color1) #打印出pink
print (color2) #打印出white
colors = [ 'red' , 'green' , 'blue' ]
new_colors = colors
new_colors[ 0 ] = 'pink'
print (new_colors) #打印出['pink', 'green', 'blue']
print (colors) #打印出['pink', 'green', 'blue']
要想实现一个列表的独立复制,可以使用切片的方式来实现。
colors = [ 'red' , 'green' , 'blue' ]
new_colors = colors[:] # 使用切片实现独立复制
new_colors[ 0 ] = 'pink'
print (new_colors) #打印出['pink', 'green', 'blue']
print (colors) #打印出['red', 'green', 'blue']
所谓的二维列表,或者说嵌套列表,其实就是列表中的元素也可以是一个列表。
例如:
numbers_1d = [ 1 , 2 , 3 ]
numbers_2d = [[ 1 , 2 , 3 ],[ 4 , 5 , 6 ],[ 7 , 8 , 9 ]]
items = [[ 0 for j in range ( 4 )] for i in range ( 3 )] #使用列表推导式生成都是0的3行4列的二维列表
二维列表的访问和操作方法和一维列表是一致的,例如要访问某个元素,只需要先访问相应的列表,再访问这个这个列表中的元素。
例如:
numbers_2d = [[ 1 , 2 , 3 ],[ 4 , 5 , 6 ],[ 7 , 8 , 9 ]]
numbers = numbers_2d[ 0 ]
print (numbers) #输出[1, 2, 3]
print (numbers[ 0 ]) #输出1
print (numbers_2d[ 0 ][ 0 ]) #也可以直接用两个方括号来访问,输出1
一维列表可以用一个 for 来实现快速遍历,那么用两个 for 就可以实现二维列表的快速遍历
numbers_2d = [[ 1 , 2 , 3 ],[ 4 , 5 , 6 ],[ 7 , 8 , 9 ]]
#索引遍历
for i in range ( len (numbers_2d)):
for j in range ( len (numbers_2d[i])):
print (numbers_2d[i][j])
print ( '------' )
#元素遍历
for i in numbers_2d:
for j in i:
print (j)
二维列表经常会引入行与列的概念,定义二维列表时,我们也可以将格式排列成行列的形式。
numbers_2d = [
[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]
]
上述代码,可以理解为第 0 行是 1,2,3,第 0 列是 1,4,7。
第 0 列 第 1 列 第 0 列 第 0 行 1 2 3 第 1 行 4 5 6 第 2 行 7 8 9