1. 列表的定义

在Pyhton中,用[]来表示列表。并用逗号来分隔其中的元素,在下方是一个简单的列表示例,这个列表包含几种自行车:

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

下方为打印结果:

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

2. 访问列表元素

在Python中,可以通过索引来访问列表元素,如:

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

输出结果为:

1
trek

在Python中,列表的索引从0开始,同时Python为访问最后一个列表元素提供了一种特殊语法。通过将索引指定为-1,可让Python返回最后一个列表元素:

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

此时的输出结果为:

1
specialized

3. 列表的改、增、删

3.1 列表元素的修改

要对列表中的值进行修改,可以用下面这种方式:

1
2
3
4
5
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

motorcycles[0] = 'ducati'
print(motorcycles)

则列表中的第一个元素’honda’被修改为’ducati’,输出如下所示:

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

3.2 列表元素的添加

3.2.1 使用方法append()在列表末尾添加元素

使用方法append()在列表末尾添加元素,如:

1
2
3
4
5
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

motorcycles.append('ducati')
print(motorcycles)

输出结果为:

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

3.2.2 使用方法insert()在列表中插入元素

使用方法insert()可以列表的任何位置添加元素,如:

1
2
3
4
motorcycles = ['honda', 'yamaha', 'suzuki']

motorcycles.insert(0, 'ducati')
print(motorcycles)

此处将’ducati’存储在索引0处,其余元素均向右移动一个位置,输出结果为:

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

3.3 在列表中删除元素

3.3.1 使用def语句删除元素(根据索引删除)

如果知道要删除的元素在列表中的位置,可使用del语句,如:

1
2
3
4
5
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

del motorcycles[0]
print(motorcycles)

此处用del删除了列表中索引为0的元素’honda’,输出结果为:

1
2
['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']

3.2.2 使用pop()方法删除元素(根据索引删除,可将删除值再利用)

有时候,需要将元素从列表中删除,并接着使用它的值,就可以使用方法pop(),它可以删除列表末尾的元素,并让你能够接着使用它,如:

1
2
3
4
5
6
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

此处将列表末尾元素’suzuki’删除,并将其存储在变量poped_motorcycle中,输出结果为:

1
2
3
['honda', 'yamaha', 'suzuki'] 
['honda', 'yamaha']
suzuki

也可以用pop()方法弹出列表中任意位置处的元素,只需要在方法的括号中指定要删除的元素的索引即可,如:

1
2
3
4
motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0)

print('The first motorcycle I owned was a ' + first_owned.title() + '.')

此处将列表中索引为0的元素’honda’弹出,并赋给变量first_owned,输出结果为:

1
The first motorcycle I owned was a Honda.

3.3.3 使用remove()方法删除元素(根据值删除元素)

有时候,你不知道要从列表中删除的值的索引,如果你只知道要删除的元素的值,则可以使用方法remove(),如:

1
2
3
4
5
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] 
print(motorcycles)

motorcycles.remove('ducati')
print(motorcycles)

此处将列表元素’ducati’从列表中删除,使用remove()方法删除元素时,也可接着使用它的值,下边删除值’ducati’,并打印一条消息,指出要将其从列表中删除的原因:

1
2
3
4
5
6
7
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.")

输出结果为:

1
2
3
4
['honda', 'yamaha', 'suzuki', 'ducati'] 
['honda', 'yamaha', 'suzuki']

A Ducati is too expensive for me.

4. 列表的排序和求长度

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

假设你有一个汽车列表,并要让其中的汽车按字母顺序排序,如:

1
2
3
cars = ['bmw', 'audi', 'toyota', 'subaru'] 
cars.sort()
print(cars)

现在,汽车是按字母顺序排列的,并且再也无法恢复到原来的排列顺序,输出结果为:

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

你还可以按与字母顺序想法的顺序排列列表元素,如:

1
2
3
cars = ['bmw', 'audi', 'toyota', 'subaru'] 
cars.sort(reverse=True)
print(cars)

则输出结果为:

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

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

要保留列表元素原来的排列顺序,同时以特定的顺序呈现它们,可使 用函数sorted()。函数sorted()让你能够按特定顺序显示列表元 素,同时不影响它们在列表中的原始排列顺序。如:

1
2
3
4
5
6
7
8
9
10
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)

则输出结果为:

1
2
3
4
5
6
7
8
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()后,列表元素的排列顺序并没有变。如果你要按与字母顺序相反的顺序显示列表,也可向函数 sorted()传递参数reverse=True。

4.3 使用方法reverse()对列表顺序永久倒转

要反转列表元素的排列顺序,可使用方法reverse(),如:

1
2
3
4
5
cars = ['bmw', 'audi', 'toyota', 'subaru'] 
print(cars)

cars.reverse()
print(cars)

输出结果为:

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

4.4 使用函数len()快速获取列表长度

使用函数len()可快速获悉列表的长度。在下面的示例中,列表包含4 个元素,因此其长度为4:

1
2
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(len(cars))

此处输出结果为:

1
4