《Python编程,从入门到实践》读书笔记——第3章:列表简介

列表是什么

列表由一系列按特定顺序排列的元素组成。鉴于列表通常包含多个元素,给列表指定一个表示复数的名称(如letters、digits或names)是个不错的主意。
在python中,用方括号([])来表示列表,并用逗号来分隔其中的元素。

#打印列表
bicycles=['trek','cannondale','redline','specialized']
print(bicycles)

['trek','cannondale','redline','specialized']

访问列表元素

要访问列表元素,可指出列表的名称,再指出元素的索引,并将其放在方括号内。

bicycles=['trek','cannondale','redline','specialized']
print(bicycles[0]#此时Python只返回该元素,而不包括方括号和引号
trek

同样可以对任何列表元素调用第2章介绍的字符串方法,如bicycles[0].title().

索引从0而不是1开始

在Python中,第一个列表元素的索引为0,而不是1。

bicycles=['trek','cannondale','redline','specialized']
print(bicycles[1])
print(bicycles[3])

cannondale
specialized

还可以通过将索引指定为-1,让python返回最后一个列表元素。这种语法在不知道列表长度时非常有用。同时,这种约定也适用于其他负数索引。如,索引-2返回倒数第二个列表元素等。

使用列表中的各个值

可像使用其它变量一样使用列表中的各个值。

bicycles=['trek','cannondale','redline','specialized']
message='My first bicycle was a '+bicycles[0].title()+'.'
print(message)

My first bicycle was a Trek.

修改、添加和删除元素

修改列表元素

修改列表元素,通过指定列表名和要修改的元素的索引,再指定该元素的新值。

**motorcycles.py**
motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
motorcycles[0]='ducati'
print(motorcycles)

['honda','yamaha','suzuki']
['ducati','yamaha','suzuki']

在列表中添加元素

1.在列表末尾添加元素: 方法append()

motorcycles.append('dacati')
print(motorcycles)

['honda','yamaha','suzuki','dacati']

使用方法append(),动态地创建列表:

motorcycles[]
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)

['honda','yamaha','suzuki']

2.在列表中插入元素
在列表的任何位置添加新元素:方法insert()

motorcycles.insert(0,'ducati')

['ducati','honda','yamaha','suzuki']

从列表中删除元素

1.语句del: 删除指定位置处的元素;把值从列表中删除后,将无法再访问它。

motorcycles = ['honda','yamaha','suzuki']
print(motorcycles)
del motorcycles[1]
print(motorcycles)

['honda','yamaha','suzuki']
['honda','suzuki']

2.方法pop(): 删除列表中任意位置处的元素;仍能继续使用被删除元素。
若括号中不指定索引,则删除列表中最后一个元素:

motorcycles = ['honda','yamaha','suzuki']
last_owned = motorcycles.pop()
print('The last motorcycles I owned was a '+last_owned.title()+'.')

The last motorcycles I owned was a Suzuki.

在括号中指定要删除的元素:

motorcycles = ['honda','yamaha','suzuki']
first_owned = motorcycles.pop(0)
print('The first motorcycle I owned was a '+first_owned.title()+'.')


The first motorcycle I owned was a Honda.

3.方法remove():根据值删除元素;被删除得值仍能使用。

motorcycles = ['honda','yamaha','suzuki','ducati']
print(motorcycles)
too_expensive='ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print('\nA'+too_expensive.title()+' is too expensive for me.')

['honda','yamaha','suzuki','ducati']
['honda','yamaha','suzuki']
A Ducati is too expensive for me.

注意:方法remove()只能删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来判断是否删除了所有这样的值。

组织列表

使用方法sort()对列表进行永久性排序

方法sort():将列表元素按照字母顺序永久性地修改。

**cars.py**
cars = ['bmw','audi','toyota','subaru']
cars.sort()
print(cars)

['audi','bmw','subaru','toyota']

向方法sort()传递参数reverse=True: 将列表元素按照与字母顺序相反的顺序永久性地修改。

cars.sort(reverse=True)
print(cars)

['toyota','subaru','bmw','audi']

使用函数sorted()对列表进行临时排序

函数sorted():将列表元素按字母顺序排序;同时不影响原始排列顺序。

cars = ['bmw','audi','toyota','subaru']
print('Here is the original list:')
print(cars)
print('\nHere is the sorted list:')
print(sorted(cars))
print('\nHere is the original list again:')
print(cars)

Here is the original list:
['bmw','audi','toyota','subaru']
Here is the sorted list:
['audi','bmw','subaru','toyota']
Here is the original list again:
['bmw','audi','toyota','subaru']

向函数sorted()传递参数reverse=True,按照与字母顺序相反的顺序显示列表,仍然不改变原始列表顺序。

print('Here is the reversed list:')
print(sorted(cars,reverse=Ture))

Here is the reversed list:
['toyota','subaru','bmw','audi']

注意:当列表元素的值并非都是小写时,按字母顺序排列列表要复杂些。

方法reverse()倒着打印列表

方法reverse():反转列表元素的排列顺序;永久性地修改,但又可以通过再次调用reverse()随时恢复到原来顺序。

cars.reverse()
print(cars)

['subaru','toyota','audi','bmw']

函数len()确定列表长度

>>>len(cars)
4

使用列表时避免索引错误

列表的索引是从0开始的。当发生索引错误时,意味着Python无法理解被指定的索引。这时可以尝试将制定的索引减1,再运行。
每当需要访问最后一个元素时,都可以使用索引-1,。这种方式只有当列表为空时,才会导致错误。
注意:发生索引错误却找不到解决办法时,可以尝试将列表或列表长度打印出来。列表可能与认为的截然不同,在程序对其进行了动态处理时尤其如此。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值