第三章 列表简介
In [30]:
#用方括号[]表示列表,并用逗号分割器中的元素。
In [31]:
bicycles=['trek','cannondale','redline','specialized']
print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
In [32]:
bicycles=['trek','cannondale','redline','specialized']
print(bicycles[0])
trek
In [33]:
bicycles=['trek','cannondale','redline','specialized']
print(bicycles[0].title())
Trek
In [34]:
bicycles=['trek','cannondale','redline','specialized']
print(bicycles[1])
print(bicycles[3])
cannondale
specialized
In [35]:
#索引从0而不是1开始
In [36]:
#访问最后一个列表元素:索引指定为-1
#索引指定-2为倒数第二个列表元素
In [37]:
bicycles=['trek','cannondale','redline','specialized']
message=f'My first bicycle was a {bicycles[0].title()}.'
print(message)
My first bicycle was a Trek.
In [38]:
bicycles=['trek','cannondale','redline','specialized']
message=f'My first bicycle was a bicycles[0].title().' #{bicycles[0].title()}不加花括号
print(message)
My first bicycle was a bicycles[0].title().
3.2.1 修改列表元素
In [39]:
#列表僵尸动态的,将随着程序的运行增删元素。
#eg.创建游戏时,开始时候将一些外星人存储在列表中,
#然后每当外星人被射杀,都将其从列表中删除;
#每次有新的外星人出现,添加进列表。 因此整个游戏运行期间,外星人列表的长度将不断变化。
In [40]:
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
motorcycles[0]='ducati'
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']
In [41]:
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
motorcycles[0]=ducati #’ducati'不加引号‘’
print(motorcycles)
['honda', 'yamaha', 'suzuki']
---------------------------------------------------------------------------NameError Traceback (most recent call last)<ipython-input-41-81d54efe3bed> in <module> 2 print(motorcycles) 3 ----> 4 motorcycles[0]=ducati #’ducati'不加引号‘’ 5 print(motorcycles)
NameError: name 'ducati' is not defined
3.2.2 添加列表元素
In [42]:
#方法append:附加在列表末尾
In [43]:
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha', 'suzuki', 'ducati']
In [44]:
#方法insert:可在任意位置添加新元素
In [45]:
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
motorcycles.insert(0,'ducati')
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['ducati', 'honda', 'yamaha', 'suzuki']
3.2.3 删除列表元素
In [46]:
#语句del:永久性删除
In [47]:
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
del motorcycles[1]
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['honda', 'suzuki']
In [48]:
#方法pop():表示“弹出”,可以找回被删除/弹出的元素,并接着使用它(但需要以下第③步)
#pop可以删掉列表中任意位置的元素
In [49]:
motorcycles=['honda','yamaha','suzuki']
print(motorcycles)
popped_motorcycle=motorcycles.pop(0) #③
print(motorcycles) #④
print(popped_motorcycle)#⑤
#注意:④和⑤不一样,④print的是‘③等号后面的motorcycles’,即被方法改变后的变量;⑤print的是‘③等号前面的pop掉的元素’
['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']
honda
In [50]:
#方法remove():当不知道从列表中删除的值所处的位置,只知道要删除的元素的值。
#remove():一次只能删除一个指定的值。
In [51]:
motorcycles=['honda','yamaha','suzuki','ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki']
In [52]:
motorcycles=['honda','yamaha','suzuki','ducati']
print(motorcycles)
too_expensive='ducati' #该步骤类似于pop,以此保留被删除的元素
motorcycles.remove(too_expensive)
print(motorcycles)
print(f'\nA {too_expensive.title()} is too expensive for me.')
['honda', 'yamaha', 'suzuki', 'ducati']
['honda', 'yamaha', 'suzuki']
A Ducati is too expensive for me.
3.3 组织列表(排序)
In [53]:
#方法sort():按字母顺序排序;
#永久性修改元素顺序
#参数reverse=True:将列表按与字母顺序相反的顺序排列;此修改也是永久性的。
In [54]:
cars=['bmw','audi','toyata','subaru']
cars.sort()
print(cars)
['audi', 'bmw', 'subaru', 'toyata']
In [55]:
#将汽车列表按与字母顺序相反的顺序排列
cars=['bmw','audi','toyata','subaru']
cars.sort(reverse=True)
print(cars)
['toyata', 'subaru', 'bmw', 'audi']
In [56]:
#函数sorted():对序列临时排序,同时不影响他们在列表中的原始排列顺序。
#也可向函数sorted()传递参数reverse=True
In [57]:
cars=['bmw','audi','toyata','subaru']
print('Here is the original list:')
print(cars)
print('\nHere is the sorted list:')
sorted(cars)
print(sorted(cars))
print(cars)
Here is the original list:
['bmw', 'audi', 'toyata', 'subaru']
Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyata']
['bmw', 'audi', 'toyata', 'subaru']
In [58]:
#方法reverse():反转列表元素的排列顺序;永久性修改。但可以再次调用reverse(),恢复原来的排列顺序。
In [59]:
#方法reverse():反转列表元素的排列顺序
cars=['bmw','audi','toyata','subaru']
print(cars)
cars.reverse()
print(cars)
['bmw', 'audi', 'toyata', 'subaru']
['subaru', 'toyata', 'audi', 'bmw']
In [60]:
#函数len()可以快速获悉列表的长度
In [61]:
cars=['bmw','audi','toyata','subaru']
len(cars)
Out[61]:
4
In [62]:
#【tips】:注意避免一些错误:如程序发生索引错误时,请尝试索引减1。
#【tips】:每当需要访问最后一个列表元素时,都可以使用索引-1;
#当列表为空时,这种访问最后一个严肃的方式才会导致错误;
#发生索引错误却找不到解决办法时,可以通过列表长度判断。
In [63]:
#当列表为空时,这种访问最后一个严肃的方式才会导致错误。eg.
cars=[]
print(cars[-1])
---------------------------------------------------------------------------IndexError Traceback (most recent call last)<ipython-input-63-00071c411a6c> in <module> 1 #当列表为空时,这种访问最后一个严肃的方式才会导致错误。eg. 2 cars=[]----> 3 print(cars[-1])
IndexError: list index out of range