02-Python-列表

1、列表是什么

列表是由一系列按照特定顺序排列的元素组成。用[ ]来表示列表,并用逗号来分割其中的元素

1 bicycles = ['trek' , 'cannondale' , 'redline' , 'specialized'] #列表还可以用list1 = list()这个方法创建,例如tuple1 = (1, 2, 3),list1 = list(tuple1)。
2 print(bicycles) 

 

1.1、访问列表元素

列表是有序的集合,因此要访问列表的任何元素,只需要将该元素的索引即可(得到元素的数据类型和它在列表中原本的元素类型一致)。索引从"0"开始

 

1 bicycles = ['trek' , 'cannondale' , 'redline' , 'specialized']
2 
3 print(bicycles[0])  #取得的元素类型为字符串
4 print(bicycles[-1]) #返回列表最后一个元素。

 

 

2、修改、添加和删除列表元素

2.1、修改列表元素

1 motorcycles =  ['honda' , 'yamaha' , 'suzuki']
2 print(motorcycles)
3 
4 motorcycles[0] = 'ducati'  #修改第一个元素
5 print(motorcycles)

 

2.2、在列表中添加元素

1 motorcycles =  ['honda' , 'yamaha' , 'suzuki']
2 print(motorcycles)
3 
4 motorcycles.append('ducati')  #将该元素添加到列表末尾。
5 print(motorcycles)
6 
7 motorcycles.insert(0,'ducati')  #可以将该元素插入到列表第一个位置。
8 print(motorcycles)

 

2.3、从列表中删除元素

 1 #使用del语句删除元素
 2 motorcycles =  ['honda' , 'yamaha' , 'suzuki']
 3 print(motorcycles)
 4 
 5 del motorcyles[0]  #删除第一个元素
 6 print(motorcycles)
 7 
 8 motorcycles =  ['honda' , 'yamaha' , 'suzuki']
 9 print(motorcycles)
10 
11 #使用pop()方法删除元素
12 popped_motorcycles = motorcyles.pop()  #pop()方法可以删除列表的元素,但可以将该元素赋值给其他变量。pop()表示删除末尾的元素,pop(0)表示删除第一个元素。
13 print(motorcycles)
14 print(popped_motorcycles)
15 print("The last motorbycle I owned was a " + popped_motorcycles.title() + ".")
16 
17 popped_motorcycles1 = motorcyles.pop(0)  #pop(0)表示删除第一个元素。
18 print(motorcycles)
19 print(popped_motorcycles1)
20 print("The first motorbycle I owned was a " + popped_motorcycles1.title() + ".")
21 
22 
23 #根据值删除元素,使用remove()方法。remove()方法只删除第一个指定的值,如果要删除的值在列表中有多个,则不能删除其余的重复值(可以使用判断和循环来删除)。
24 motorcycles =  ['honda' , 'yamaha' , 'suzuki', 'ducati']
25 print(motorcycles)
26 
27 motorcycles.remove('ducati')
28 print(motorcycles)
29 
30 #使用remove()方法删除元素时,也可以接着使用它的值。
31 motorcycles =  ['honda' , 'yamaha' , 'suzuki', 'ducati']
32 print(motorcycles)
33 
34 too_expensive = 'ducati'  #将值存储在变量中
35 motorcycles.remove(too_expensive)  #使用变量删除值。虽然值从列表中删除,但是还保存在变量中。
36 print(motorcycles)
37 print(too_expensive)
38 print("A " + too_expensive.title() + " is too expensive for me."

 

2.4、扩展列表

1 names = ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy']
2 b = [1,2,3]
3 names.extend(b)
4 print(names)
5 ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3]

 

2.5、列表的拷贝

1 names = ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3]
2 
3 name_copy = names.copy()  #此为浅拷贝,只拷贝一层
4 print(name_copy)
5 ['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3]

 

2.6、列表元素的统计

1 names = ['Alex', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3]
2 names.count("Amy")  #统计'amy'出现的次数

 

2.7、获取index(下标)

names = ['Tom', 'Tenglan', 'Amy', 'Amy', 'Alex', '3', '2', '1']
names.index("Amy")  #结果为2,只返回找到的第一个元素的下标

 

3、组织列表

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

1 cars = ['audi' , 'bmw', 'benz' ,'toyota', 'subaru']
2 print(cars)
3 cars.sort()
4 print(cars)
5 
6 cars.sort(reverse=True)  #反向排序
7 print(cars)

 

3.2、使用sorted()方法对列表进行临时排序

 1 cars = ['audi' , 'bmw', 'benz' ,'toyota', 'subaru']
 2 
 3 print("Here is the original list: ")
 4 print(cars)
 5 
 6 print("\nHere is the sorted list: ")
 7 print(sorted(cars))  #临时排序
 8 
 9 print("\nHere is the reverse sorted list: ")
10 print(sorted(cars,reverse=True))  #临时反向排序
11 
12 print("\nHear is the original list again: ")
13 print(cars)  #原列表元素顺序没有发生改变

 

3.3、倒着打印列表

reverse()方法不是指按照与字母顺序相反的顺序排列列表元素,而只是反转列表元素的排列。

1 cars = ['audi' , 'bmw', 'benz' ,'toyota', 'subaru']
2 print(cars)
3 
4 cars.reverse()  #只会反转元素,不会对反转的元素进行排序。
5 print(cars) 

 

3.4、确定列表长度

1 cars = ['audi' , 'bmw', 'benz' ,'toyota', 'subaru']
2 len(cars)  #显示列表元素个数

 

posted @ 2017-10-16 15:58  Druid_Py  阅读(251)  评论(0编辑  收藏  举报