本节内容
列表、元组操作
字符串操作
字典操作
集合操作
文件操作
字符编码与转码
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 Xiangpeng3 ['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-315print(names[:3]) #如果是从头开始取,0可以忽略,跟上句效果一样
执行结果:
1 ['ZhangYang', 'Guyun', 'Xiangpeng', 'XuLiangChen']2 ZhangYang Xiangpeng3 ['Guyun', 'Xiangpeng']4 XuLiangChen5 Xiangpeng6 ['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") #删除Nulige14 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
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 names = ["4ZhangYang","#!Guyun","xXieDi","Xiangpeng","XuLiangChen"]6 names2 =names.copy()7 print(names)8 print(names2)
执行结果:
1 ['4ZhangYang', '#!Guyun', 'xXieDi', 'Xiangpeng', 'XuLiangChen']2 ['4ZhangYang', '#!Guyun', 'xXieDi', 'Xiangpeng', 'XuLiangChen']
拷贝示例2
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 names = ["4ZhangYang","#!Guyun","XiangPeng","ChenRonghua"]6 names2 =names.copy()7 print(names)8 print(names2)9 names[2] = "向鹏"
10 print(names)
执行结果:
1 ['4ZhangYang', '#!Guyun', 'XiangPeng', 'ChenRonghua']2 ['4ZhangYang', '#!Guyun', 'XiangPeng', 'ChenRonghua']3 ['4ZhangYang', '#!Guyun', '向鹏', 'ChenRonghua']
拷贝示例3
1、浅拷贝
示例1
浅拷贝就是浅浅的copy一层
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4 #import copy
5 names = ["4ZhangYang","#!Guyun","XiangPeng",["alex","jack"],"ChenRonghua"]6 names2 =names.copy()7 print(names)8 print(names2)9 names[2] = "向鹏"
10 names[3][0] = "ALEXANDER" #注意:复制列表,只会复制一层
11 print(names)12 print(names2)
执行结果:
1 ['4ZhangYang', '#!Guyun', 'XiangPeng', ['alex', 'jack'], 'ChenRonghua']2 ['4ZhangYang', '#!Guyun', 'XiangPeng', ['alex', 'jack'], 'ChenRonghua']3 ['4ZhangYang', '#!Guyun', '向鹏', ['ALEXANDER', 'jack'], 'ChenRonghua']4 ['4ZhangYang', '#!Guyun', 'XiangPeng', ['ALEXANDER', 'jack'], 'ChenRonghua'] #只拷贝了第一层,所以向鹏没有变
示例2
运行结果报错,后期再解决
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 importcopy6
7 person=['name',['a',100]]8 #p1=copy.deepcopy(person)
9 p1=copy.copy(person)10 p2=person[:]11 p3=list(person)
结果省略...
示例3
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 person = ['name',['saving',100]]6 p1=person[:]7 p2=person[:]8 p1[0]='alex'
9 p2[0]='fenjie'
10 p1[1][1]=50 #浅拷贝的作用,用于创建联合账号(100元,两人的两个账号各分一半,变成两个50元)
11 print(p1)12 print(p2)
执行结果:
1 ['alex', ['saving', 50]]2 ['fenjie', ['saving', 50]]
2、深拷贝
此实验还有点小问题,后期修改。
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 importcopy6
7 names = ["4ZhangYang","#!Guyun","XiangPeng",["alex","jack"],"ChenRonghua"]8 #names2 = names.copy()
9 #names2 = copy.copy(names)
10 names2 =copy.deepcopy(names)11 print(names)12 print(names2)13 names[2] = "向鹏"
14 names[3][0] = "ALEXANDER" #注意:复制列表,只会复制一层
15 print(names)16 print(names2)
执行结果:省略......
列表循环
用for实现列表循环
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 #定义一个列表
6 names = ["4ZhangYang","#!Guyun","XiangPeng",["alex","jack"],"ChenRonghua"]7 for i innames:8 print(i)
执行结果:
1 4ZhangYang2 #!Guyun
3 XiangPeng4 ['alex', 'jack']5 ChenRonghua
跳着切片示例
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 #跳着切片
6 names = ["4ZhangYang","#!Guyun","XiangPeng",["alex","jack"],"ChenRonghua","Xuliangche"]7 print(names[0:-1:2])8 print(names[::2])9 print(names[:]) #0到-1
10
11 for i innames:12 print(i)
执行结果:
1 ['4ZhangYang', 'XiangPeng', 'ChenRonghua']2 ['4ZhangYang', 'XiangPeng', 'ChenRonghua']3 ['4ZhangYang', '#!Guyun', 'XiangPeng', ['alex', 'jack'], 'ChenRonghua', 'Xuliangche']4 4ZhangYang5 #!Guyun
6 XiangPeng7 ['alex', 'jack']8 ChenRonghua9 Xuliangche
元组(也叫不可变列表)
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表。
语法
names = ("ales","jack","eric")
它只有两个方法,一个是count,一个是index。
示例:
1 #定义一个元组
2 tuple1 = (1, 2, '3', 4, '5')3 #定义了一个元组之后就无法再添加或修改元组中的元素
4 print tuple1[0] #元组的元素都有确定的顺序。元组的索引也是以0为基点的
5 print tuple1[-1] #负的索引从元组的尾部开始计数
6 print tuple1[1:3] #元组也可以进行切片操作。对元组切片可以得到新的元组。
7 #可以使用 in 运算符检查某元素是否存在于元组中。
8 print 1 in tuple1 #True
9 #使用for in 进行遍历元组
10 for item intuple1:11 printitem12 #如果需要获取item的序号 可以使用下面的遍历方法:
13 for index inrange(len(tuple1)):14 printtuple1[index]15 #还可以使用内置的enumerate函数
16 for index, item inenumerate(tuple1):17 print '%i, %s' % (index, item)
统计示例
#统计有两个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 >>>names2 ['Alex', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3]3 >>> names.sort() #排序
4 Traceback (most recent call last):5 File "", line 1, in
6 TypeError: unorderable types: int() < str() #3.0里不同数据类型不能放在一起排序了,擦
7 >>> names[-3] = '1'
8 >>> names[-2] = '2'
9 >>> names[-1] = '3'
10 >>>names11 ['Alex', 'Amy', 'Amy', 'Tenglan', 'Tom', '1', '2', '3']12 >>>names.sort()13 >>>names14 ['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 delnames211 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
4 print(names,names2)5 NameError: name 'names2' is not defined
字符串操作
1 >>> a = 'alex'
2 >>> a [1]3 'l'
4 >>> a[2]5 'e'
6
7 #备注:列表不能直接赋值
8
9 >>> a[2] = '222'
10 Traceback (most recent call last):11 File "", line 1, in
12 TypeError: 'str' object does not support item assignment
集合操作
集合的元素是不允许重复且无序的集合,集合就像是字典舍弃了值一样。
主要作用如下:
去重,把一个列表变成集合,就自动去重了
关系测试,测试两组数据之前的交集、差集、并集等关系
1 s = set([3,5,9,10]) #创建一个数值集合
2
3 t = set("Hello") #创建一个唯一字符的集合
4
5
6 a = t | s #t 和 s的并集
7
8 b = t & s #t 和 s的交集
9
10 c = t – s #求差集(项在t中,但不在s中)
11
12 d = t ^ s #对称差集(项在t或s中,但不会同时出现在二者中)
13
14
15
16 基本操作:17
18 t.add('x') #添加一项
19
20 s.update([10,37,42]) #在s中添加多项
21
22
23
24 使用remove()可以删除一项:25
26 t.remove('H')27
28
29 len(s)30 set 的长度31
32 x ins33 测试 x 是否是 s 的成员34
35 x not ins36 测试 x 是否不是 s 的成员37
38 s.issubset(t)39 s <=t40 测试是否 s 中的每一个元素都在 t 中41
42 s.issuperset(t)43 s >=t44 测试是否 t 中的每一个元素都在 s 中45
46 s.union(t)47 s |t48 返回一个新的 set 包含 s 和 t 中的每一个元素49
50 s.intersection(t)51 s &t52 返回一个新的 set 包含 s 和 t 中的公共元素53
54 s.difference(t)55 s -t56 返回一个新的 set 包含 s 中有但是 t 中没有的元素57
58 s.symmetric_difference(t)59 s ^t60 返回一个新的 set 包含 s 和 t 中不重复的元素61
62 s.copy()63 返回 set “s”的一个浅复制
常用操作
交集(intersection)
两组列表中,取相同的值
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 list_1 = [1,4,5,7,3,6,7,9]6 list_1 =set(list_1)7 print(list_1,type(list_1))8
9 list_2 = ([2,6,0,66,22,8,4])10 print(list_1,list_2)11
12 #交集
13 print( list_1.intersection(list_2) )
执行结果:
1 {1, 3, 4, 5, 6, 7, 9}
2 {1, 3, 4, 5, 6, 7, 9} [2, 6, 0, 66, 22, 8, 4]3 {4, 6}
并集(union)
两个列表合并成一个,去掉重复的值
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 list_1 = [1,4,5,7,3,6,7,9]6 list_1 =set(list_1)7 print(list_1,type(list_1))8
9 list_2 = ([2,6,0,66,22,8,4])10 print(list_1,list_2)11
12 #并集
13 print(list_1.union(list_2))
执行结果:
1 {1, 3, 4, 5, 6, 7, 9}
2 {1, 3, 4, 5, 6, 7, 9} [2, 6, 0, 66, 22, 8, 4]3 {0, 1, 2, 3, 4, 5, 6, 7, 66, 9, 8, 22}
差集(difference)
只保留list1里面有的,list2中没有的值。
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 list_1 = [1,4,5,7,3,6,7,9]6 list_1 =set(list_1)7 print(list_1,type(list_1))8
9 list_2 = ([2,6,0,66,22,8,4])10 print(list_1,list_2)11
12 #差集
13 print(list_1.difference(list_2))
14 print(list_2.difference(list_1))
执行结果:
1 {1, 3, 4, 5, 6, 7, 9}
2 {1, 3, 4, 5, 6, 7, 9} [2, 6, 0, 66, 22, 8, 4]3 {1, 3, 5, 7, 9}
4 {0,8,2,66,22}
对称差集(symmetric_difference)
把交集去掉了,留下的不是并集,把两个互相都没有的,取出来。
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 list_1 = [1,4,5,7,3,6,7,9]6 list_1 =set(list_1)7
8 list_2 = ([2,6,0,66,22,8,4])9 print(list_1,list_2)10
11
12 #反向差集
13 print(list_1.symmetric_difference(list_2))
执行结果:
1 {1, 3, 4, 5, 6, 7, 9} [2, 6, 0, 66, 22, 8, 4]2 {0, 1, 2, 66, 3, 5, 8, 7, 9, 22}
isdisjoint
判断有没有交集,如果有返回False,否则返回True
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 list_1 = [1,4,5,7,3,6,7,9]6 list_1 =set(list_1)7
8 list_2 = ([2,6,0,66,22,8,4])9 print(list_1,list_2)10
11 #子集
12 list_3 = set([1,3,7])13 print(list_3.issubset(list_1))14 print(list_1.issubset(list_3))15
16 print("---------------------------------")
17 list_4 = set([5,6,8])
18 print(list_3.isdisjoint(list_4))
执行结果:
1 {1, 3, 4, 5, 6, 7, 9} [2, 6, 0, 66, 22, 8, 4]2 True3 False4 ---------------------------------
5 True
判断有没有交集,如果有返回False,否则返回True
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 list_1 = [1,4,5,7,3,6,7,9]6 list_1 =set(list_1)7
8 list_2 = ([2,6,0,66,22,8,4])9 print(list_1,list_2)10
11 #子集
12 list_3 = set([1,3,7])13 print(list_3.issubset(list_1))14 print(list_1.issubset(list_3))15
16 print("---------------------------------")
17 list_4 = set([5,6,7,8])
18 print(list_3.isdisjoint(list_4))
执行结果:
1 {1, 3, 4, 5, 6, 7, 9} [2, 6, 0, 66, 22, 8, 4]2 True3 False4 ---------------------------------
5 False
子集(issubset)
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 list_1 = [1,4,5,7,3,6,7,9]6 list_1 =set(list_1)7
8 list_2 = ([2,6,0,66,22,8,4])9 print(list_1,list_2)10
11 #差集
12 print( list_1.intersection(list_2))13
14 #子集
15 list_3 = set([1,3,7])16 print(list_3.issubset(list_1))17 print(list_1.issubset(list_3))
执行结果:
1 {1, 3, 4, 5, 6, 7, 9} [2, 6, 0, 66, 22, 8, 4]2 {4, 6}3 True4 False
交集、并集、差集、对称差集另一种用法
1 #交集(intersection)
2 print(list_1 &list_2)3
4 #并集 union
5 print(list_2 |list_1)6
7 #差集 difference (in list 1 but not in list 2)
8 print(list_1 -list_2)9
10 #对称差集
11 print(list_1 ^ list_2)
常用操作
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #Author: nulige
4
5 list_1 = [1,4,5,7,3,6,7,9]6 list_1 =set(list_1)7
8 list_2 = ([2,6,0,66,22,8,4])9 print(list_1,list_2)10
11 #添加一项(无序插入)
12 list_1.add(999)13 print(list_1)14
15 #添加多项
16 list_1.update([888,777,555])17 print(list_1)18
19 #pop (删除)
20 #Remove and teturn an arbitrary set element.
21 #删除一个,并返回一个任意的
22 print(list_1.pop())23 print(list_1.pop())24 print(list_1.pop())25 print(list_1.pop())26
27 #discard
28 print( list_1.discard(888) )
执行结果:
1 {1, 3, 4, 5, 6, 7, 9} [2, 6, 0, 66, 22, 8, 4]2 {1, 3, 4, 5, 6, 7, 999, 9}3 {1, 3, 4, 5, 6, 7, 999, 9, 777, 555, 888}4 1
5 3
6 4
7 5
8 None