首页 > 代码库 > Day2 - Python基础(列表,元组等)

Day2 - Python基础(列表,元组等)

本节内容

  1. 列表、元组操作
  2. 字符串操作
  3. 字典操作
  4. 集合操作
  5. 文件操作
  6. 字符编码与转码 

1. 列表、元组操作

列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作

 

定义列表示例

1 names = ["ZhangYang"," Guyun"," Xiangpeng"," XuLiangChen"]

通过下标访问列表中的元素,下标从0开始计数

1 >>> names[0]
2 ZhangYang

 

切片示例1

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author: nulige
 4 
 5 #!/usr/bin/env python
 6 # -*- coding:utf-8 -*-
 7 #Author: huzhihua
 8 
 9 names = "ZhangYang Guyun Xiangpeng XuLiangChen"
10 names = ["ZhangYang"," Guyun"," Xiangpeng"," XuLiangChen"]
11 
12 print(names)
13 print(names[0],names[2])
14 print(names[1:3])       #这就是切片(从0开始切,0就是ZhangYang)

执行结果:

1 [ZhangYang,  Guyun,  Xiangpeng,  XuLiangChen]
2 ZhangYang  Xiangpeng
3 [ Guyun,  Xiangpeng]

 

切片示例2

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author: nulige
 4 
 5 names = "ZhangYang Guyun Xiangpeng XuLiangChen"
 6 names = ["ZhangYang"," Guyun"," Xiangpeng"," XuLiangChen"]
 7 
 8 print(names)
 9 print(names[0],names[2])
10 print(names[1:3])  #这就是切片(从0开始,取下标1至下标3之间的数字,包括1,不包括3)
11print(names[3]) #取第3个值,从0从始取
12print(names[-2]) #在不知道是多长的情况下,取出最后2个位置
13print(names[-2:]) #要取最后面的值,把-1省略掉
14print(names[0:3]) #取从0-3
15print(names[:3]) #如果是从头开始取,0可以忽略,跟上句效果一样

执行结果:

1 [ZhangYang,  Guyun,  Xiangpeng,  XuLiangChen]
2 ZhangYang  Xiangpeng
3 [ Guyun,  Xiangpeng]
4  XuLiangChen
5  Xiangpeng
6 [ Xiangpeng,  XuLiangChen]
7 [ZhangYang,  Guyun,  Xiangpeng]
8 [ZhangYang,  Guyun,  Xiangpeng]

 

追加示例

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 #Author: nulige
4 
5 names = "ZhangYang Guyun Xiangpeng XuLiangChen"
6 names = ["ZhangYang"," Guyun"," Xiangpeng"," XuLiangChen"]
7 names.append("LeiHaidong")   #追加到最后一行
8 print(names)

执行结果:

1 [ZhangYang,  Guyun,  Xiangpeng,  XuLiangChen, LeiHaidong]

 

插入示例

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author: nulige
 4 
 5 names = "ZhangYang Guyun Xiangpeng XuLiangChen"
 6 names = ["ZhangYang"," Guyun"," Xiangpeng"," XuLiangChen"]
 7 names.append("LeiHaidong")   #追加到最后一行
 8 names.insert(1,"nulige")     #插到那个位置 就写那个位置
 9 names.insert(3,"Xinzhiyu")   #插入3的位置,不能批量插入
10 print(names)

执行结果:

1 [ZhangYang, nulige,  Guyun, ‘Xinzhiyu Xiangpeng,  XuLiangChen, LeiHaidong]

 

修改示例

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author: nulige
 4 
 5 names = "ZhangYang Guyun Xiangpeng XuLiangChen"
 6 names = ["ZhangYang"," Guyun"," Xiangpeng"," XuLiangChen"]
 7 names.append("LeiHaidong")   #追加到最后一行
 8 names.insert(1,"nulige")     #插到那个位置 就写那个位置
 9 names.insert(3,"Xinzhiyu")   #插入3的位置,不能批量插入
10 names[2] ="XieDi"        #修改
11 print(names)

执行结果:

1 [ZhangYang, nulige, ‘XieDi‘, Xinzhiyu,  Xiangpeng,  XuLiangChen, LeiHaidong]

 

删除示例

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author: nulige
 4 
 5 names = "ZhangYang Guyun Xiangpeng XuLiangChen"
 6 names = ["ZhangYang"," Guyun"," Xiangpeng"," XuLiangChen"]
 7 names.append("LeiHaidong")   #追加到最后一行
 8 names.insert(1,"Nulige")     #插到那个位置 就写那个位置
 9 names.insert(3,"Xinzhiyu")   #插入3的位置,不能批量插入
10 names[2] ="XieDi"
11 
12 #delete
13 names.remove("Nulige")   #删除Nulige
14 del names[1]
15 print(names)

执行效果:

删除前效果:

[ZhangYang, ‘Nulige‘, XieDi, Xinzhiyu,  Xiangpeng,  XuLiangChen, LeiHaidong]

删除后效果:

1 [ZhangYang, Xinzhiyu,  Xiangpeng,  XuLiangChen, LeiHaidong]

index示例

查找XieDi的位置,并将找到的结果打印出来
 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author: nulige
 4 
 5 names = "ZhangYang Guyun Xiangpeng XuLiangChen"
 6 names = ["ZhangYang"," Guyun","XieDi"," Xiangpeng"," XuLiangChen"]
 7 
 8 # 查找XieDi的位置,并将找到的结果打印出来
 9 print(names)
10 print(names.index("XieDi"))           #找到XieDi
11 print(names[names.index("XieDi")])    #把他打印出来

执行结果:

1 [ZhangYang,  Guyun, XieDi,  Xiangpeng,  XuLiangChen]
2 2
3 XieDi

 

拷贝示例

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 #Author: nulige
4 
5 names = "ZhangYang Guyun Xiangpeng XuLiangChen"
6 names = ["ZhangYang"," Guyun","XieDi"," Xiangpeng"," XuLiangChen"]
7 
8 name_copy = names.copy()
9 print(name_copy)

执行结果:

1 [ZhangYang,  Guyun, XieDi,  Xiangpeng,  XuLiangChen]

copy真的这么简单么?

 

统计示例

#统计有两个XieDi

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author: nulige
 4 
 5 names = "ZhangYang Guyun Xiangpeng XuLiangChen "
 6 names = ["ZhangYang"," Guyun","XieDi"," Xiangpeng"," XuLiangChen"]
 7 names.append("LeiHaidong")
 8 names.insert(1,"ChenRonghua")
 9 names.insert(3,"Xinzhiyu")   #插入
10 names[2] ="XieDi"            #修改
11 print(names)
12 print(names.count("XieDi"))

执行结果:

1 [ZhangYang, ChenRonghua, XieDi, Xinzhiyu, XieDi,  Xiangpeng,  XuLiangChen, LeiHaidong]
2 2

 

清除示例

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author: nulige
 4 
 5 names = "ZhangYang Guyun Xiangpeng XuLiangChen "
 6 names = ["ZhangYang"," Guyun","XieDi"," Xiangpeng"," XuLiangChen"]
 7 names.append("LeiHaidong")
 8 names.insert(1,"ChenRonghua")
 9 names.insert(3,"Xinzhiyu")   #插入
10 names[2] ="XieDi"            #修改
11 print(names)
12 print(names.count("XieDi"))
13 
14 #clear
15 names.clear()
16 print(names)

执行结果

1 [ZhangYang, ChenRonghua, XieDi, Xinzhiyu, XieDi,  Xiangpeng,  XuLiangChen, LeiHaidong]
2 2
3 []   

 

反转示例1

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author: nulige
 4 
 5 names = "ZhangYang Guyun Xiangpeng XuLiangChen "
 6 names = ["ZhangYang"," Guyun","XieDi"," Xiangpeng"," XuLiangChen"]
 7 names.append("LeiHaidong")
 8 names.insert(1,"ChenRonghua")
 9 names.insert(3,"Xinzhiyu")   #插入
10 names[2] ="XieDi"            #修改
11 print(names)
12 print(names.count("XieDi"))
13 
14 #reverse
15 names.reverse()
16 print(names)

执行结果:

1 [ZhangYang, ChenRonghua, XieDi, Xinzhiyu, XieDi,  Xiangpeng,  XuLiangChen, LeiHaidong]    
2 2
3 [LeiHaidong,  XuLiangChen,  Xiangpeng, XieDi, Xinzhiyu, XieDi, ChenRonghua, ZhangYang]    #对比第一行,反转过来了

 

反转示例2

 1 >>> names
 2 [Alex, Tenglan, Amy, Tom, Amy, 1, 2, 3]
 3 >>> names.sort() #排序
 4 Traceback (most recent call last):
 5   File "<stdin>", line 1, in <module>
 6 TypeError: unorderable types: int() < str()   #3.0里不同数据类型不能放在一起排序了,擦
 7 >>> names[-3] = 1
 8 >>> names[-2] = 2
 9 >>> names[-1] = 3
10 >>> names
11 [Alex, Amy, Amy, Tenglan, Tom, 1, 2, 3]
12 >>> names.sort()
13 >>> names
14 [1, 2, 3, Alex, Amy, Amy, Tenglan, Tom]

 

排序示例

按ASCII码顺序进行排序的

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author: nulige
 4 
 5 names = ["4ZhangYang"," #!Guyun","xXieDi"," Xiangpeng"," XuLiangChen"]
 6 print(names)
 7 
 8 #排序(sort)
 9 names.sort()
10 print(names)

执行结果:

1 [4ZhangYang,  #!Guyun, xXieDi,  Xiangpeng,  XuLiangChen]
2 [ #!Guyun,  Xiangpeng,  XuLiangChen, 4ZhangYang, xXieDi]

 

扩展示例

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author: nulige
 4 
 5 names = ["4ZhangYang"," #!Guyun","xXieDi"," Xiangpeng"," XuLiangChen"]
 6 print(names)
 7 
 8 names2 = [1,2,3,4]
 9 names.extend(names2)
10 print(names)

执行结果:

1 [4ZhangYang,  #!Guyun, xXieDi,  Xiangpeng,  XuLiangChen]
2 [4ZhangYang,  #!Guyun, xXieDi,  Xiangpeng,  XuLiangChen, 1, 2, 3, 4]

 

删除列表中的变量示例

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author: nulige
 4 
 5 names = ["4ZhangYang"," #!Guyun","xXieDi"," Xiangpeng"," XuLiangChen"]
 6 print(names)
 7 
 8 names2 = [1,2,3,4]
 9 names.extend(names2)
10 del names2
11 print(names,names2)

执行结果:

会报错,因为找不到nemes2变量的值

1 Traceback (most recent call last):
2 [4ZhangYang,  #!Guyun, xXieDi,  Xiangpeng,  XuLiangChen]
3   File "D:/python/day2/name_test02.py", line 11, in <module>
4     print(names,names2)
5 NameError: name names2 is not defined

 

 

 

 

 

 

 

 

 

 

 

 

Day2 - Python基础(列表,元组等)