一. 课上代码
>>> member = ['Jack', 'David', 'Hsin', 'Chi']
>>> member
['Jack', 'David', 'Hsin', 'Chi']
>>> number = [1, 2, 3, 4, 5]
>>> number
[1, 2, 3, 4, 5]
>>> mix = [1, 'Jack', 3.14, [1, 2, 3]]
>>> mix
[1, 'Jack', 3.14, [1, 2, 3]]
>>> empty = []
>>> empty
[]
>>> member
['Jack', 'David', 'Hsin', 'Chi']
>>> member.append('Shelly')
>>> member
['Jack', 'David', 'Hsin', 'Chi', 'Shelly']
#append作为一种添加元素的方法
>>> len(member)
5
>>> member.append('fishc', 'fish')
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
member.append('fishc', 'fish')
TypeError: append() takes exactly one argument (2 given)
>>> member.extend('fishc', 'fish')
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
member.extend('fishc', 'fish')
TypeError: extend() takes exactly one argument (2 given)
#append不能一次添加两个元素
#extend可以一次添加几个元素,但是需要以数组的方式添加
>>> member.extend(['fishc', 'fish'])
>>> member
['Jack', 'David', 'Hsin', 'Chi', 'Shelly', 'fishc', 'fish']
>>> len(member)
7
>>> member.insert(1, 'Rose')
>>> member
['Jack', 'Rose', 'David', 'Hsin', 'Chi', 'Shelly', 'fishc', 'fish']
>>> member.insert(0, 'Titanic')
#insert是第三种添加元素的方法,并且可以添加到特定位置
>>> member
['Titanic', 'Jack', 'Rose', 'David', 'Hsin', 'Chi', 'Shelly', 'fishc', 'fish']
>>>
二. 测试题
- member.append(['David', 'Mike'])和member.extend(['David', 'Mike'])实现的效果一样吗?
>>> member = ['Jack', 'Rose']
>>> member.append(['David', 'Mike'])
>>> member
['Jack', 'Rose', ['David', 'Mike']]
>>> member = ['Jack', 'Rose']
>>> member.extend(['David', 'Mike'])
>>> member
['Jack', 'Rose', 'David', 'Mike']
- 有列表name = ['F', 'i', 'h', 'C'],如果想要在元素'i'和'h'之间插入元素's',应该使用什么方法插入?
>>> name = ['F', 'i', 'h', 'C']
>>> name.insert(1, 's')
>>> name
['F', 's', 'i', 'h', 'C']
#注意数字代表的位置
>>> name = ['F', 'i', 'h', 'C']
>>> name.insert(2, 's')
>>> name
['F', 'i', 's', 'h', 'C']
三. 动动手
- 假定给定一下列表:
member = ['fish', 'dark', 'road', 'quiet', 'sun']
要求将列表修改为:
member = ['fish', 88, 'dark', 90, 'road', 85, 'quiet', 90, 'sun', 88]
方法一:使用insert()和append()方法修改列表
方法二:重新创建一个同名字的列表覆盖
>>> member = ['fish', 'dark', 'road', 'quiet', 'sun']
>>> member1 = member[0]
>>> member2 = member[1]
>>> member3 = member[2]
>>> member4 = member[3]
>>> member5 = member[4]
>>> member1.append(88)
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
member1.append(88)
AttributeError: 'str' object has no attribute 'append'
>>> member1.extend(88)
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
member1.extend(88)
AttributeError: 'str' object has no attribute 'extend'
>>> member1
'fish'
>>> member1.append('88')
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
member1.append('88')
AttributeError: 'str' object has no attribute 'append'
>>> member1 = member(0)
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
member1 = member(0)
TypeError: 'list' object is not callable
>>> member.append(88)
>>> member
['fish', 'dark', 'road', 'quiet', 'sun', 88]
>>> member1 = [member[0]]
>>> member1
['fish']
>>> member1.append(88)
>>> member1 = member[0]
>>> member2 = member[1]
>>> member3 = member[2]
>>> member4 = member[3]
>>> member5 = member[4]
SyntaxError: multiple statements found while compiling a single statement
>>> member1 = [member[0]]
>>> member2 = [member[1]]
>>> member3 = [member[2]]
>>> member4 = [member[3]]
>>> member5 = [member[4]]
SyntaxError: multiple statements found while compiling a single statement
>>> member = ['fish', 'dark', 'road', 'quiet', 'sun']
>>> member1 = [member[0]]
>>> member2 = [member[1]]
>>> member3 = [member[2]]
>>> member4 = [member[3]]
>>> member5 = [member[4]]
#需要把member[4]变成列表之后才能添加元素
>>> member1.append(88)
>>> member2.append(90)
>>> member3.append(85)
>>> member4.append(90)
>>> member5.append(88)
>>> member = member1 + member2 + member3 + member4 + member5
>>> member
['fish', 88, 'dark', 90, 'road', 85, 'quiet', 90, 'sun', 88]
>>> member = ['fish', 'dark', 'road', 'quiet', 'sun']
>>> member.insert(0, 88, 1, 90, 2, 85, 3, 90, 4, 88)
Traceback (most recent call last):
File "<pyshell#45>", line 1, in <module>
member.insert(0, 88, 1, 90, 2, 85, 3, 90, 4, 88)
TypeError: insert() takes exactly 2 arguments (10 given)
>>> member.insert(0, 88)
>>> member
[88, 'fish', 'dark', 'road', 'quiet', 'sun']
>>> member = ['fish', 'dark', 'road', 'quiet', 'sun']
>>> member.insert(1, 88)
>>> member
['fish', 88, 'dark', 'road', 'quiet', 'sun']
>>> member.insert(3, 90)
#用insert()方法每次添加完元素之后,之后的元素位置都会改变
>>> member
['fish', 88, 'dark', 90, 'road', 'quiet', 'sun']
>>> member.insert(5, 85)
>>> member
['fish', 88, 'dark', 90, 'road', 85, 'quiet', 'sun']
>>> member.insert(7, 90)
>>> member
['fish', 88, 'dark', 90, 'road', 85, 'quiet', 90, 'sun']
>>> member.insert(9, 88)
>>> member
['fish', 88, 'dark', 90, 'road', 85, 'quiet', 90, 'sun', 88]
>>> member = ['fish', 'dark', 'road', 'quiet', 'sun']
>>> member = ['fish', 88, 'dark', 90, 'road', 85, 'quiet', 90, 'sun', 88]
#直接覆盖的方法最为简单
>>> member
['fish', 88, 'dark', 90, 'road', 85, 'quiet', 90, 'sun', 88]
-
利用for循环打印上边member列表中的每个内容,如图:
>>> for i in range(member):
print("i\n")
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
for i in range(member):
TypeError: 'list' object cannot be interpreted as an integer
>>> for i in range(member):
print("i"\n)
SyntaxError: unexpected character after line continuation character
>>> for i in range(member):
print("i",\n)
SyntaxError: unexpected character after line continuation character
>>> for i in range(member):
print(i)
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
for i in range(member):
TypeError: 'list' object cannot be interpreted as an integer
#此处不需要给member加上range
>>> for i in member:
print(i)
fish
88
dark
90
road
85
quiet
90
sun
88
-
能否修改一下改成如图所示(使用两种方法):
#第一种方法
member = ['fish', 88, 'dark', 90, 'road', 85, 'quiet', 90, 'sun', 88]
i = 0
while i <= 8:
print(member[i:i+2])
i += 2
网友评论