目录
一, 列表的定义
列表数据类型,用处十分广泛,主要表现在:
(1)列表中的内容可以是不同的类型,基本上所有Python支持的数据类型,都可以作为其元素。内容广泛。
(2)列表是有顺序的; 据此我们可以进行索引访问。
(3)列表示可变的; insert remove append etc...。
(4)列表是用逗号分隔元素,用中括号括起来。[元素1,元素2,...]
定义列表,举例:
my_list = [123,'abc',3.1415,[1,2,3]] #多种元素的列表
my_list = list() #定义空列表
my_list1 = [] #定义空列表
二, 列表的操作
(2.1)下标定位法
另外注意,下标也可采用负值。例如:a = my_list[-1] // -1 = len(list)+ (-1)
(2.2)列表的分片
my_list = [123,'abc',3.1415,[1,2,3]] #多种元素的列表
l_1 = my_list[1:3] #下标为1:2的元素集合
l_2 = my_list[1:-1] #下标为1:4 的元素集合
l_3 = my_list[2:] #下标为2:最后一个元素的集合
l_4 = my_list[::2] #下标每隔2个元素的集合
print (l_1)
print (l_2)
print (l_3)
print (l_4)
print(l_1 ,l_2 ,l_3 ,l_4)
###########################################
['abc', 3.1415]
['abc', 3.1415]
[3.1415, [1, 2, 3]]
[123, 3.1415]
['abc', 3.1415] ['abc', 3.1415] [3.1415, [1, 2, 3]] [123, 3.1415]
(2.3)列表的循环
for i in my_list:
print(i)
##### result ######
123
abc
3.1415
[1, 2, 3]
[Finished in 154ms]
(2.4)range 函数生成列表
for i in range(5):
print(i)
for i in range(0,5):
print(i)
#########################
0
1
2
3
4
0
1
2
3
4
[Finished in 156ms]
(2.5)列表添加元素
(A)使用append 函数,追加。
a.append(b):是将b原样追加到a的末尾上,会改变a的值,其中,b可为列表、元组、字符串、一串数/字符/字符串。
A.1 追加单个元素
my_list = [1, 2, 3, 4, 5]
my_list.append(6) # 在末尾添加元素 6 , 注意,不可以返回一个列表,会是NONE
my_list.insert(3,7) # 在下标为3 的地方,添加元素7
##############################
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 7, 4, 5, 6]
[Finished in 229ms]
A.2 追加不同其它元素
(B)将一个列表中的内容,添加到另一个列表中,使用extend方法。
a.extend(b):是将b拆解后追加到a的末尾上,会改变a的值,其中,b可为列表、元组、字符串、一串数/字符/字符串。
备注:字符串和元组中都没有append()和extend()函数
(2.6)列表删除元素
python删除列表中元素的方法:
1、使用remove()删除单个元素,该函数可以删除列表中某个值的第一个匹配项;
2、使用pop()删除单个或多个元素,该函数根据索引来删除元素,并返回该元素的值;
注意:str.pop() 代表默认删除最后一个元素。
3、使用del关键字根据索引来删除元素
(2.7)列表清空:
(2.8)列表长度获取
list_length = len(my_list)
(2.9)列表的排序
python中排序有两个函数第一个是sort(),第二个是sorted(),sort函数对序列进行永久性的改变,sorted函数在排序后会生成一个新的序列,原序列不变。
lst=[1,3,2]
lst.sort() // 默认升序排列
print lst
# 输出结果如下
# [1,2,3]
lst=[1,3,2]
lst.sort(reverse=Ture) // 指定降序排列
print lst
# 输出结果如下
# [3,2,1]
lst=[1,3,2]
print (sorted(lst))
pritn lst
# 输出结果如下
# [1,2,3]
# [1,3,2]
sorted()用法总结:
sorted(list),这种方法直接将传入的list进行排序,返回升序排序的顺序。
sorted(list,reverse=True),这种方法将传入的list排序,返回降序序列。
sorted进阶用法:在使用sorted对一个list进行排序时,传入一个key值来实现自定义的排序。
sorted进阶用法:在使用sorted对一个list进行排序时,传入一个key值来实现自定义的排序。
sorted([1,8,5,-7],key=abs)
# 返回[1,5,7,8]
这里key会依次作用于list中的每一个元素上,实际上不会改变list中元素的值,根据key函数返回的结果进行排序。
sorted(['abc','Abc','Cba','bAc'],key=str.lower())
# 结果返回['abc','Abc','bAc','Cba']
sorted更高级的用法:sorted可以结合lambda进行排序,通过lambda可以实现条件更复杂的排序:
实现对嵌套list或者元组list排序:
sorted([['guo','A','25'],['shao','a','24'],['zhang','b','26'],['wang','c','33']],key=lambda x:x[2])
# 输出结果为
# [['shao', 'a', '24'], ['guo', 'A', '25'], ['zhang', 'b', '26'], ['wang', 'c', '33']]
my_list = [['guo','A','25'],['shao','a','24'],['zhang','b','26'],['wang','c','33']]
sorted(my_list,key=lambda x:x[1])
print (my_list)
# 输出结果为
[['guo', 'A', '25'], ['shao', 'a', '24'], ['zhang', 'b', '26'], ['wang', 'c', '33']]
(2.10)列表的复制
my_list1 = [1,2,3,4,5]
my_list2 = my_list1;
my_list1.append(6)
print (my_list2)
### 结果:
[1,2,3,4,5,6] // 两个列表句柄,指向同一个列表对象,具有同一份数值
(a)浅拷贝
Python中,可以使用 copy() 函数来复制 python 列表,此时original_list
和copyed_list
指向内存中不同的列表对象。
original_list=[1,2,3]
#Copying list using copy function
copied_list=original_list.copy() // 使用copy 函数
print(copied_list)
#Output:[1, 2, 3]
print(original_list)
#Output:[1, 2, 3]
#checking the id of both original and copied list
print(id(original_list))
#Output:27800264
print(id(copied_list))
#Output:27799880
此外,使用copy 模块,可以实现相同的功能:
import copy
original_list = [1,2,3] #Copying list using copy function
copied_list = copy.copy(original_list)
original_list.append(4)
print(copied_list) #Output:[1, 2, 3]
print(original_list) #Output:[1, 2, 3]
#checking the id of both original and copied list
print(id(original_list)) #Output:27800264
print(id(copied_list)) #Output:27799880
###### 结果:
[1, 2, 3]
[1, 2, 3, 4]
2884366260224
2884366056256
[Finished in 158ms]
再次拓展。子列表中元素,也会跟着copy,这里是一个陷阱,请注意。
import copy
s_list = ['a','b','c']
original_list = [1,2,3] #Copying list using copy function
original_list.append(s_list)
copied_list = copy.copy(original_list)
original_list.append(4)
s_list.append(5)
print(copied_list) #Output:[1, 2, 3]
print(original_list) #Output:[1, 2, 3]
#checking the id of both original and copied list
print(id(original_list)) #Output:27800264
print(id(copied_list)) #Output:27799880
###### 结果:
[1, 2, 3, ['a', 'b', 'c', 5]] // 结果发现,s_list 列表中添加一个元素之后,
[1, 2, 3, ['a', 'b', 'c', 5], 4] // original_list & copied_list 中均含有 元素5
2254301445312
2254301445376
[Finished in 165ms]
(b)深拷贝
针对(a)中的相互牵连的问题,可以采用深拷贝。例子如下:
import copy
s_list = ['a','b','c']
original_list = [1,2,3] #Copying list using copy function
original_list.append(s_list)
copied_list = copy.deepcopy(original_list)
original_list.append(4)
s_list.append(5)
print(copied_list) #Output:[1, 2, 3]
print(original_list) #Output:[1, 2, 3]
#checking the id of both original and copied list
print(id(original_list)) #Output:27800264
print(id(copied_list)) #Output:27799880
###### 结果:
[1, 2, 3, ['a', 'b', 'c']]
[1, 2, 3, ['a', 'b', 'c', 5], 4]
1954649947392
1954649947456
[Finished in 249ms]