3.1 列表元素的索引
在Python中, 第一个列表元素的索引为0, 而不是1。 在大多数编程语言中都是如此, 这与列表操作的底层实现相关。
3.2 修改、 添加和删除元素
- 修改列表元素:直接索引修改
- 列表中添加元素:
- 列表末尾添加:使用方法append()
- 列表之中插入元素:使用方法insert()
01 motorcycles = ['honda', 'yamaha', 'suzuki']; 02 print(motorcycles); 03 motorcycles.append('ducati'); 04 print(motorcycles); 05 motorcycles.insert(0, 'ducati') 06 print(motorcycles)
>>> ['honda', 'yamaha', 'suzuki'] ['honda', 'yamaha', 'suzuki', 'ducati'] ['ducati', 'honda', 'yamaha', 'suzuki', 'ducati'] |
- 列表中删除元素:
- 使用del语句;
- 使用方法pop()——删除列表末尾的元素相当于弹出栈顶元素,可以继续使用这个值
01 motorcycles = ['honda', 'yamaha', 'suzuki'] 02 print(motorcycles) 03 del motorcycles[0] 04 print(motorcycles) 05 06 motorcycles = ['honda', 'yamaha', 'suzuki'] 07 print(motorcycles) 08 popped_motorcycle = motorcycles.pop() 09 print(motorcycles) 10 print(popped_motorcycle)
>>> ['honda', 'yamaha', 'suzuki'] ['yamaha', 'suzuki'] ['honda', 'yamaha', 'suzuki'] ['honda', 'yamaha'] suzuki |
- 弹出列表中任何位置的元素:方法pop()
01 motorcycles = ['honda', 'yamaha', 'suzuki'] 02 first_owned = motorcycles.pop(0) 03 print('The first motorcycle I owned was a ' + first_owned.title() + '.')
>>>The first motorcycle I owned was a Honda. |
- 根据值删除元素
01 motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati'] 02 print(motorcycles) 03 motorcycles.remove('ducati') 04 print(motorcycles)
>>> ['honda', 'yamaha', 'suzuki', 'ducati'] ['honda', 'yamaha', 'suzuki'] |
3.3 组织列表
- 使用方法sort()对列表进行永久性按字母顺序排序
按与字母顺序相反的顺序排列列表元素, 为此, 只需向sort() 方法传递参数reverse=True
01 cars = ['bmw', 'audi', 'toyota', 'subaru'] 02 cars.sort() 03 print(cars) 04 cars = ['bmw', 'audi', 'toyota', 'subaru'] 05 cars.sort(reverse=True) 06 print(cars)
>>> ['audi', 'bmw', 'subaru', 'toyota'] ['toyota', 'subaru', 'bmw', 'audi'] |
- 使用函数sorted对函数进行临时排序
01 cars = ['bmw', 'audi', 'toyota', 'subaru'] 02 print("Here is the original list:") 03 print(cars) 04 print("\nHere is the sorted list:") 05 print(sorted(cars)) 06 print("\nHere is the original list again:") 07 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'] |
- 倒着打印列表:方法reverse()
- 确定列表的长度:函数len()
01 cars = ['bmw', 'audi', 'toyota', 'subaru'] 02 print("Here is the original list:") 03 print(cars) 04 print("\nHere is the sorted list:") 05 print(sorted(cars)) 06 print("\nHere is the original list again:") 07 print(cars) 08 09 length=len(cars); 10 print(str(length));
>>> 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'] 4 |
3.4 正确使用列表索引
访问最后一个元素,要记得索引是整个序列长度-1;
索引-1 总是返回最后一个列表元素,但是列表为空时会报错:
01 motorcycles = ['honda', 'yamaha', 'suzuki']; 02 print(motorcycles[-1]); 03 04 none=[]; 05 print(none[-1]);
suzuki Traceback (most recent call last): File "C:/Users/Franz/Desktop/untitled.py", line 5, in <module> print(none[-1]); IndexError: list index out of range |
注意:每当你使用pop() 时, 被弹出的元素就不再在列表中了。
注意 :方法remove() 只删除第一个指定的值。 如果要删除的值可能在列表中出现多次, 就需要使用循环来判断是否删除了所有这样的值。