第4章_列表
1、列表连接和列表复制
操作符+可以连接两个列表,操作符*可以用于一个列表和一个整数,实现列表的复制
[ 1 , 2 , 3 ] + [ 'A' , 'B' , 'C' ]
>> >> > [ 1 , 2 , 3 , 'A' , 'B' , 'C' ]
[ 'X' , 'Y' , 'Z' ] * 3
>> >> > [ 'X' , 'Y' , 'Z' , 'X' , 'Y' , 'Z' , 'X' , 'Y' , 'Z' ]
2、用del语句从列表中删除值
spam = [ 'cat' , 'bat' , 'rat' , 'elephant' ]
del spam[ 2 ]
print ( spam)
>> >> > [ 'cat' , 'bat' , 'elephant' ]
3、in和not in操作符
'hello' in [ 'hello' , 'hi' , 'cat' ]
>> >> > True
'dog' not in [ 'hello' , 'hi' , 'cat' ]
>> >> > True
4、多重赋值技巧
cat = [ 'fat' , 'black' , 'loud' ]
size, color, disposition = cat
5、index()方法返回下表
cat = [ 'fat' , 'black' , 'loud' ]
print ( cat. index( 'black' ) )
>> >> > 1
6、append()和insert()方法在列表中添加值
append():将参数添加到末尾; insert():可以在列表任意下标出插入一个值
spam = [ 'cat' , 'bat' , 'bat' ]
spam. append( 'moose' )
spam. insert( 1 , 'chicken' )
print ( spam)
>> >> > [ 'cat' , 'chicken' , 'bat' , 'bat' , 'moose' ]
7、remove()方法从列表中删除值
spam = [ 'cat' , 'chicken' , 'bat' , 'bat' , 'moose' ]
spam. remove( 'cat' )
print ( spam)
>> >> > [ 'chicken' , 'bat' , 'bat' , 'moose' ]
8、sort()方法将列表中的值排序
sort()排序, 默认升序,不能对既有数字又有字符串的列表排序。
cat = [ 'fat' , 'black' , 'loud' ]
cat. sort( )
print ( cat)
>> >> > [ 'black' , 'fat' , 'loud' ]
cat. sort( reverse= True )
print ( cat)
>> >> > [ 'loud' , 'fat' , 'black' ]
spam = [ 'a' , 'z' , 'A' , 'Z' ]
spam. sort( )
print ( spam)
>> >> > [ 'A' , 'Z' , 'a' , 'z' ]
spam. sort( key= str . lower)
print ( spam)
>> >> > [ 'A' , 'a' , 'Z' , 'z' ]
9、类似列表的类型:字符串和元组
10、可变和不可变数据类型
name = 'zophie'
name[ 3 ] = 't'
>> >> > TypeError: 'str' object does not support item assignment
eggs = ( 'hello' , 42 , 0.5 )
eggs[ 0 ] = 'world'
>> >> > TypeError: 'tuple' object does not support item assignment
元组只有一个值时,可以在括号内该值的后面跟上一个逗号,表明是元组 。
type ( ( 'hello' ) )
>> >> > str
type ( ( 'hello' , ) )
>> >> > tuple
11、重点了解:引用
在变量必须保存可变数据类型的值时,例如列表或字典,Python就使用引用。对于不可变的数据类型的值时,例如字符串、整型或元组,Python变量就保存值本身。 当你将列表赋给一个变量时,实际上是将列表的“引用”赋给了该变量。如下代码,只改变了cheese列表,但cheese和spam列表同时发生了改变。
spam = [ 0 , 1 , 2 , 3 , 4 , 5 ]
cheese = spam
cheese[ 1 ] = 'Hello'
print ( spam)
>> >> > [ 0 , 'Hello' , 2 , 3 , 4 , 5 ]
print ( cheese)
>> >> > [ 0 , 'Hello' , 2 , 3 , 4 , 5 ]
12、重点了解:传递引用
当eggs()被调用时,没有使用返回值来为spam赋新值。相反,它直接当场修改了该列表。
def eggs ( some_lis) :
some_lis. append( 'Hello' )
spam = [ 1 , 2 , 3 ]
eggs( spam)
print ( spam)
>> >> > [ 1 , 2 , 3 , 'Hello' ]
13、重点了解:copy()和deepcopy()函数
如果函数修改了传入的列表或字典,你可能不希望这些变动影响原来的列表或字典,可用copy.copy()来复制列表或字典这样的可变值,而不是复制引用。
import copy
spam = [ 'A' , 'B' , 'C' , 'D' ]
cheese = copy. copy( spam)
cheese[ 1 ] = 42
print ( spam)
>> >> > [ 'A' , 'B' , 'C' , 'D' ]
print ( cheese)
>> >> > [ 'A' , 42 , 'C' , 'D' ]
如果复制到列表中包含了列表,就用copy.deepcopy()来代替; copy.copy()和[:]都是浅拷贝;copy.deepcopy()是深拷贝。浅拷贝对于列表的子列表还是引用。
import copy
spam = [ 'A' , [ 'cat' , 'dog' ] , 'C' , 'D' ]
q_copy = copy. copy( spam)
q_copy[ 1 ] [ 0 ] = '浅拷贝'
>> >> > print ( q_copy)
[ 'A' , [ '浅拷贝' , 'dog' ] , 'C' , 'D' ]
print ( spam)
>> >> > [ 'A' , [ '浅拷贝' , 'dog' ] , 'C' , 'D' ]
spam = [ 'A' , [ 'cat' , 'dog' ] , 'C' , 'D' ]
s_copy = copy. deepcopy( spam)
s_copy[ 1 ] [ 0 ] = '深拷贝'
print ( s_copy)
>> >> > [ 'A' , [ '深拷贝' , 'dog' ] , 'C' , 'D' ]
print ( spam)
>> >> > [ 'A' , [ 'cat' , 'dog' ] , 'C' , 'D' ]
14、小项目-逗号代码
以列表值作为参数,返回一个字符串,字符串之间以逗号分隔,并在最后一个之前插入and。
def fun ( lis) :
for parm in lis[ : - 1 ] :
print ( parm, end= ',' )
print ( 'and' , lis[ - 1 ] )
spam = [ 'apples' , 'bananas' , 'tofu' , 'cats' ]
fun( spam)
>> >> > apples, bananas, tofu, and cats
15、小项目- 字符图网络
grid = [ [ '.' , '.' , '.' , '.' , '.' , '.' ] ,
[ '.' , '0' , '0' , '.' , '.' , '.' ] ,
[ '0' , '0' , '0' , '0' , '.' , '.' ] ,
[ '0' , '0' , '0' , '0' , '0' , '.' ] ,
[ '.' , '0' , '0' , '0' , '0' , '0' ] ,
[ '0' , '0' , '0' , '0' , '0' , '.' ] ,
[ '0' , '0' , '0' , '0' , '.' , '.' ] ,
[ '.' , '0' , '0' , '.' , '.' , '.' ] ,
[ '.' , '.' , '.' , '.' , '.' , '.' ] ]
for i in range ( len ( grid[ 0 ] ) ) :
for j in range ( len ( grid) ) :
if j == len ( grid) - 1 :
print ( grid[ j] [ i] )
else :
print ( grid[ j] [ i] , end= '' )