圆月山庄资源网 Design By www.vgjia.com
集合是无序的,天生不重复的数据组合,它的作用如下:
- 去重,即:把一个列表变成集合,就去重了
- 关系测试,即:测试两组集合的交集、并集和差集等
一、集合常用方法总结
二、定义
1、语法
> name_1 = [1,2,3,4,7,8,7,10] #把列表转换为集合 > name_1 = set(name_1) #转换后,去重 > print(name_1,type(name_1)) {1, 2, 3, 4, 7, 8, 10} <class 'set'>
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#输出结果
> name_1.intersection(name_2)
{8, 1, 10, 3}
2、并集(union())
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#输出结果
> name_1.union(name_2)
{1, 2, 3, 4, 5, 7, 8, 10}
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#输出结果
> name_1.difference(name_2)
{2, 4, 7}
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_3 = [1,2,3,4]
> name_1 = set(name_1)
> name_3 = set(name_3)
#输出结果
> name_3.issubset(name_1)
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_3 = [1,2,3,4]
> name_1 = set(name_1)
> name_3 = set(name_3)
#输出结果
> name_1.issuperset(name_3)
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#输出结果
> name_1.symmetric_difference(name_2)
{2, 4, 5, 7}
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_3 = [11]
> name_1 = set(name_1)
> name_2 = set(name_2)
> name_3 = set(name_3)
#有交集
> name_1.isdisjoint(name_2)
False
#无交集
> name_1.isdisjoint(name_3)
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#结果输出
> name_1 & name_2
{8, 1, 10, 3}
2、并集(|)
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#结果输出
> name_1 | name_2
{1, 2, 3, 4, 5, 7, 8, 10}
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#结果输出
> name_1 - name_2
{2, 4, 7}
4、对称差集(^)
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#输出
> name_1 ^ name_2
{2, 4, 5, 7}
5、是否是子集(<=)
> name_1 = [1,2,3,4,7,8,10]
> name_3 = [1,2,3,4]
> name_1 = set(name_1)
> name_3 = set(name_3)
#输出
> name_3 <= name_1
True
6、是否是父集(>=)
> name_1 = [1,2,3,4,7,8,10]
> name_3 = [1,2,3,4]
> name_1 = set(name_1)
> name_3 = set(name_3)
#输出
> name_1 >= name_3
True
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
#添加已存在,不报错
> name_2.add(1)
> name_2
{8, 1, 10, 3, 5}
#添加不存在,添加一个新的数值
> name_2.add(11)
> name_2
{1, 3, 5, 8, 10, 11}
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2.update([12,13,14])
#输出结果
> name_2
{1, 3, 5, 8, 10, 12, 13, 14}
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2
{8, 1, 10, 3, 5}
> name_2.remove(1)
#输出
> name_2
{8, 10, 3, 5}
#删除不存在的元素,会报错
> name_2.remove(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 1
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2
{8, 1, 10, 3, 5}
#输出
> name_2.pop()
8
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2.discard(10)
#输出结果
> name_2
{8, 1, 3, 5}
#删除不存在元素,不报错
> name_2.discard(10)
特别提示:用discard删除不存在的元素,不会出现报错
4、长度(len())
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> len(name_1)
7
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> 1 in name_1
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#输出
> 12 not in name_1
True
> name_1 = [1,2,3,4,7,8,7,10] > name_2 = [1,3,5,8,10] > name_1 = set(name_1) > name_2 = set(name_2) #输出结果 > name_1.intersection(name_2) {8, 1, 10, 3}
> name_1 = [1,2,3,4,7,8,7,10] > name_2 = [1,3,5,8,10] > name_1 = set(name_1) > name_2 = set(name_2) #输出结果 > name_1.union(name_2) {1, 2, 3, 4, 5, 7, 8, 10}
> name_1 = [1,2,3,4,7,8,7,10] > name_2 = [1,3,5,8,10] > name_1 = set(name_1) > name_2 = set(name_2) #输出结果 > name_1.difference(name_2) {2, 4, 7}
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_3 = [1,2,3,4]
> name_1 = set(name_1)
> name_3 = set(name_3)
#输出结果
> name_3.issubset(name_1)
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_3 = [1,2,3,4]
> name_1 = set(name_1)
> name_3 = set(name_3)
#输出结果
> name_1.issuperset(name_3)
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#输出结果
> name_1.symmetric_difference(name_2)
{2, 4, 5, 7}
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_3 = [11]
> name_1 = set(name_1)
> name_2 = set(name_2)
> name_3 = set(name_3)
#有交集
> name_1.isdisjoint(name_2)
False
#无交集
> name_1.isdisjoint(name_3)
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#结果输出
> name_1 & name_2
{8, 1, 10, 3}
2、并集(|)
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#结果输出
> name_1 | name_2
{1, 2, 3, 4, 5, 7, 8, 10}
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#结果输出
> name_1 - name_2
{2, 4, 7}
4、对称差集(^)
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#输出
> name_1 ^ name_2
{2, 4, 5, 7}
5、是否是子集(<=)
> name_1 = [1,2,3,4,7,8,10]
> name_3 = [1,2,3,4]
> name_1 = set(name_1)
> name_3 = set(name_3)
#输出
> name_3 <= name_1
True
6、是否是父集(>=)
> name_1 = [1,2,3,4,7,8,10]
> name_3 = [1,2,3,4]
> name_1 = set(name_1)
> name_3 = set(name_3)
#输出
> name_1 >= name_3
True
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
#添加已存在,不报错
> name_2.add(1)
> name_2
{8, 1, 10, 3, 5}
#添加不存在,添加一个新的数值
> name_2.add(11)
> name_2
{1, 3, 5, 8, 10, 11}
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2.update([12,13,14])
#输出结果
> name_2
{1, 3, 5, 8, 10, 12, 13, 14}
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2
{8, 1, 10, 3, 5}
> name_2.remove(1)
#输出
> name_2
{8, 10, 3, 5}
#删除不存在的元素,会报错
> name_2.remove(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 1
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2
{8, 1, 10, 3, 5}
#输出
> name_2.pop()
8
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2.discard(10)
#输出结果
> name_2
{8, 1, 3, 5}
#删除不存在元素,不报错
> name_2.discard(10)
特别提示:用discard删除不存在的元素,不会出现报错
4、长度(len())
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> len(name_1)
7
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> 1 in name_1
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#输出
> 12 not in name_1
True
> name_1 = [1,2,3,4,7,8,7,10] > name_3 = [1,2,3,4] > name_1 = set(name_1) > name_3 = set(name_3) #输出结果 > name_3.issubset(name_1) True
> name_1 = [1,2,3,4,7,8,7,10] > name_3 = [1,2,3,4] > name_1 = set(name_1) > name_3 = set(name_3) #输出结果 > name_1.issuperset(name_3) True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#输出结果
> name_1.symmetric_difference(name_2)
{2, 4, 5, 7}
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_3 = [11]
> name_1 = set(name_1)
> name_2 = set(name_2)
> name_3 = set(name_3)
#有交集
> name_1.isdisjoint(name_2)
False
#无交集
> name_1.isdisjoint(name_3)
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#结果输出
> name_1 & name_2
{8, 1, 10, 3}
2、并集(|)
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#结果输出
> name_1 | name_2
{1, 2, 3, 4, 5, 7, 8, 10}
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#结果输出
> name_1 - name_2
{2, 4, 7}
4、对称差集(^)
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#输出
> name_1 ^ name_2
{2, 4, 5, 7}
5、是否是子集(<=)
> name_1 = [1,2,3,4,7,8,10]
> name_3 = [1,2,3,4]
> name_1 = set(name_1)
> name_3 = set(name_3)
#输出
> name_3 <= name_1
True
6、是否是父集(>=)
> name_1 = [1,2,3,4,7,8,10]
> name_3 = [1,2,3,4]
> name_1 = set(name_1)
> name_3 = set(name_3)
#输出
> name_1 >= name_3
True
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
#添加已存在,不报错
> name_2.add(1)
> name_2
{8, 1, 10, 3, 5}
#添加不存在,添加一个新的数值
> name_2.add(11)
> name_2
{1, 3, 5, 8, 10, 11}
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2.update([12,13,14])
#输出结果
> name_2
{1, 3, 5, 8, 10, 12, 13, 14}
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2
{8, 1, 10, 3, 5}
> name_2.remove(1)
#输出
> name_2
{8, 10, 3, 5}
#删除不存在的元素,会报错
> name_2.remove(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 1
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2
{8, 1, 10, 3, 5}
#输出
> name_2.pop()
8
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2.discard(10)
#输出结果
> name_2
{8, 1, 3, 5}
#删除不存在元素,不报错
> name_2.discard(10)
特别提示:用discard删除不存在的元素,不会出现报错
4、长度(len())
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> len(name_1)
7
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> 1 in name_1
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#输出
> 12 not in name_1
True
> name_1 = [1,2,3,4,7,8,7,10] > name_2 = [1,3,5,8,10] > name_1 = set(name_1) > name_2 = set(name_2) #输出结果 > name_1.symmetric_difference(name_2) {2, 4, 5, 7}
> name_1 = [1,2,3,4,7,8,7,10] > name_2 = [1,3,5,8,10] > name_3 = [11] > name_1 = set(name_1) > name_2 = set(name_2) > name_3 = set(name_3) #有交集 > name_1.isdisjoint(name_2) False #无交集 > name_1.isdisjoint(name_3) True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#结果输出
> name_1 & name_2
{8, 1, 10, 3}
2、并集(|)
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#结果输出
> name_1 | name_2
{1, 2, 3, 4, 5, 7, 8, 10}
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#结果输出
> name_1 - name_2
{2, 4, 7}
4、对称差集(^)
> name_1 = [1,2,3,4,7,8,7,10]
> name_2 = [1,3,5,8,10]
> name_1 = set(name_1)
> name_2 = set(name_2)
#输出
> name_1 ^ name_2
{2, 4, 5, 7}
5、是否是子集(<=)
> name_1 = [1,2,3,4,7,8,10]
> name_3 = [1,2,3,4]
> name_1 = set(name_1)
> name_3 = set(name_3)
#输出
> name_3 <= name_1
True
6、是否是父集(>=)
> name_1 = [1,2,3,4,7,8,10]
> name_3 = [1,2,3,4]
> name_1 = set(name_1)
> name_3 = set(name_3)
#输出
> name_1 >= name_3
True
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
#添加已存在,不报错
> name_2.add(1)
> name_2
{8, 1, 10, 3, 5}
#添加不存在,添加一个新的数值
> name_2.add(11)
> name_2
{1, 3, 5, 8, 10, 11}
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2.update([12,13,14])
#输出结果
> name_2
{1, 3, 5, 8, 10, 12, 13, 14}
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2
{8, 1, 10, 3, 5}
> name_2.remove(1)
#输出
> name_2
{8, 10, 3, 5}
#删除不存在的元素,会报错
> name_2.remove(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 1
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2
{8, 1, 10, 3, 5}
#输出
> name_2.pop()
8
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2.discard(10)
#输出结果
> name_2
{8, 1, 3, 5}
#删除不存在元素,不报错
> name_2.discard(10)
特别提示:用discard删除不存在的元素,不会出现报错
4、长度(len())
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> len(name_1)
7
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> 1 in name_1
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#输出
> 12 not in name_1
True
> name_1 = [1,2,3,4,7,8,7,10] > name_2 = [1,3,5,8,10] > name_1 = set(name_1) > name_2 = set(name_2) #结果输出 > name_1 & name_2 {8, 1, 10, 3}
> name_1 = [1,2,3,4,7,8,7,10] > name_2 = [1,3,5,8,10] > name_1 = set(name_1) > name_2 = set(name_2) #结果输出 > name_1 | name_2 {1, 2, 3, 4, 5, 7, 8, 10}
> name_1 = [1,2,3,4,7,8,7,10] > name_2 = [1,3,5,8,10] > name_1 = set(name_1) > name_2 = set(name_2) #结果输出 > name_1 - name_2 {2, 4, 7}
4、对称差集(^)
> name_1 = [1,2,3,4,7,8,7,10] > name_2 = [1,3,5,8,10] > name_1 = set(name_1) > name_2 = set(name_2) #输出 > name_1 ^ name_2 {2, 4, 5, 7}
5、是否是子集(<=)
> name_1 = [1,2,3,4,7,8,10] > name_3 = [1,2,3,4] > name_1 = set(name_1) > name_3 = set(name_3) #输出 > name_3 <= name_1 True
6、是否是父集(>=)
> name_1 = [1,2,3,4,7,8,10] > name_3 = [1,2,3,4] > name_1 = set(name_1) > name_3 = set(name_3) #输出 > name_1 >= name_3 True
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
#添加已存在,不报错
> name_2.add(1)
> name_2
{8, 1, 10, 3, 5}
#添加不存在,添加一个新的数值
> name_2.add(11)
> name_2
{1, 3, 5, 8, 10, 11}
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2.update([12,13,14])
#输出结果
> name_2
{1, 3, 5, 8, 10, 12, 13, 14}
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2
{8, 1, 10, 3, 5}
> name_2.remove(1)
#输出
> name_2
{8, 10, 3, 5}
#删除不存在的元素,会报错
> name_2.remove(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 1
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2
{8, 1, 10, 3, 5}
#输出
> name_2.pop()
8
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2.discard(10)
#输出结果
> name_2
{8, 1, 3, 5}
#删除不存在元素,不报错
> name_2.discard(10)
特别提示:用discard删除不存在的元素,不会出现报错
4、长度(len())
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> len(name_1)
7
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> 1 in name_1
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#输出
> 12 not in name_1
True
> name_2 = [1,3,5,8,10] > name_2 = set(name_2) #添加已存在,不报错 > name_2.add(1) > name_2 {8, 1, 10, 3, 5} #添加不存在,添加一个新的数值 > name_2.add(11) > name_2 {1, 3, 5, 8, 10, 11}
> name_2 = [1,3,5,8,10] > name_2 = set(name_2) > name_2.update([12,13,14]) #输出结果 > name_2 {1, 3, 5, 8, 10, 12, 13, 14}
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2
{8, 1, 10, 3, 5}
> name_2.remove(1)
#输出
> name_2
{8, 10, 3, 5}
#删除不存在的元素,会报错
> name_2.remove(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 1
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2
{8, 1, 10, 3, 5}
#输出
> name_2.pop()
8
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2.discard(10)
#输出结果
> name_2
{8, 1, 3, 5}
#删除不存在元素,不报错
> name_2.discard(10)
特别提示:用discard删除不存在的元素,不会出现报错
4、长度(len())
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> len(name_1)
7
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> 1 in name_1
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#输出
> 12 not in name_1
True
> name_2 = [1,3,5,8,10] > name_2 = set(name_2) > name_2 {8, 1, 10, 3, 5} > name_2.remove(1) #输出 > name_2 {8, 10, 3, 5} #删除不存在的元素,会报错 > name_2.remove(1) Traceback (most recent call last): File "<input>", line 1, in <module> KeyError: 1
> name_2 = [1,3,5,8,10] > name_2 = set(name_2) > name_2 {8, 1, 10, 3, 5} #输出 > name_2.pop() 8
"htmlcode">
> name_2 = [1,3,5,8,10]
> name_2 = set(name_2)
> name_2.discard(10)
#输出结果
> name_2
{8, 1, 3, 5}
#删除不存在元素,不报错
> name_2.discard(10)
特别提示:用discard删除不存在的元素,不会出现报错
4、长度(len())
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> len(name_1)
7
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#结果输出
> 1 in name_1
True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#输出
> 12 not in name_1
True
> name_2 = [1,3,5,8,10] > name_2 = set(name_2) > name_2.discard(10) #输出结果 > name_2 {8, 1, 3, 5} #删除不存在元素,不报错 > name_2.discard(10)
> name_1 = [1,2,3,4,7,8,7,10] > name_1 = set(name_1) #结果输出 > len(name_1) 7
> name_1 = [1,2,3,4,7,8,7,10] > name_1 = set(name_1) #结果输出 > 1 in name_1 True
"htmlcode">
> name_1 = [1,2,3,4,7,8,7,10]
> name_1 = set(name_1)
#输出
> 12 not in name_1
True
> name_1 = [1,2,3,4,7,8,7,10] > name_1 = set(name_1) #输出 > 12 not in name_1 True
更多关于Python集合操作方法可查看下面的相关链接
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月02日
2024年11月02日
- 明达年度发烧碟MasterSuperiorAudiophile2021[DSF]
- 英文DJ 《致命的温柔》24K德国HD金碟DTS 2CD[WAV+分轨][1.7G]
- 张学友1997《不老的传说》宝丽金首版 [WAV+CUE][971M]
- 张韶涵2024 《不负韶华》开盘母带[低速原抓WAV+CUE][1.1G]
- lol全球总决赛lcs三号种子是谁 S14全球总决赛lcs三号种子队伍介绍
- lol全球总决赛lck三号种子是谁 S14全球总决赛lck三号种子队伍
- 群星.2005-三里屯音乐之男孩女孩的情人节【太合麦田】【WAV+CUE】
- 崔健.2005-给你一点颜色【东西音乐】【WAV+CUE】
- 南台湾小姑娘.1998-心爱,等一下【大旗】【WAV+CUE】
- 【新世纪】群星-美丽人生(CestLaVie)(6CD)[WAV+CUE]
- ProteanQuartet-Tempusomniavincit(2024)[24-WAV]
- SirEdwardElgarconductsElgar[FLAC+CUE]
- 田震《20世纪中华歌坛名人百集珍藏版》[WAV+CUE][1G]
- BEYOND《大地》24K金蝶限量编号[低速原抓WAV+CUE][986M]
- 陈奕迅《准备中 SACD》[日本限量版] [WAV+CUE][1.2G]