Python的set集合浅析

set是一个无序且不重复的元素集合

set的优点:访问速度快;天生解决重复问题

部分源码分析如下:

1 def add(self, *args, **kwargs): # real signature unknown
2         """
3         Add an element to a set.
4
5         This has no effect if the element is already present.
6         """
7         pass
 1 #练习1.添加
 2 s1 = set()
 3 s1.add("11,22,33")    #可以添加字符串和元组,不能添加列表,字典,每次只能添加一个
 4 s1.add("kkkkkkkk")
 5 print(s1)
 6 s1.add("11,22,33")      #添加重复字符串不生效
 7 s1.add(("k1","v1"))
 8 print(s1)
 9
10 #执行结果:
11 {‘11,22,33‘, ‘kkkkkkkk‘}
12 {‘11,22,33‘, ‘kkkkkkkk‘, (‘k1‘, ‘v1‘)}注意:另外一种方法可以添加列表,字典
1 s2 = set([44,55,66,"aaa"])     #感觉像是赋值,因为后面被覆盖了
2 print(s2)
3 s2 = set({"kk":"vv","jj":"oo"})    #如果是字典取key
4 print(s2)
5
6 执行结果:
7 {66, ‘aaa‘, 44, 55}
8 {‘kk‘, ‘jj‘}
1 def clear(self, *args, **kwargs): # real signature unknown
2         """ Remove all elements from this set. """
3         pass
 1 #练习2.清空set集合
 2 s1 = set()
 3 s1.add("11,22,33")    #可以添加字符串和元组,不能添加列表,字典,每次只能添加一个
 4 print(s1)
 5 s1.clear()     #清空set集合了
 6 print(s1)
 7
 8 #执行结果:
 9 {‘11,22,33‘}
10 set()
1 def difference(self, *args, **kwargs): # real signature unknown
2         """
3         Return the difference of two or more sets as a new set.
4
5         (i.e. all elements that are in this set but not the others.)
6         """
7         pass
1 #练习3.比较不同的部分放到新集合
2 s2 = set(["aaa","bbb","ccc","ddd","aaa"])
3 print(s2)
4 ret = s2.difference(["aaa","bbb"])    #输出与指定集合不相同的部分放到新集合,无序
5 print(ret)
6
7 执行结果:
8 {‘aaa‘, ‘ddd‘, ‘bbb‘, ‘ccc‘}
9 {‘ccc‘, ‘ddd‘}
def difference_update(self, *args, **kwargs): # real signature unknown    """ Remove all elements of another set from this set. """    pass
 1 #练习4.更新原有集合,移除相同部分
 2 s2 = set(["aaa","bbb","ccc","ddd","aaa"])
 3 print(s2)
 4 ret = s2.difference_update(["aaa","bbb"])    #更新原来的集合,移除相同部分,不生成集合
 5 print(s2)
 6 print(ret)      #不生成新集合,所以返回了None
 7
 8 #执行结果:
 9 {‘ddd‘, ‘bbb‘, ‘ccc‘, ‘aaa‘}
10 {‘ccc‘, ‘ddd‘}
11 None
1 def discard(self, *args, **kwargs): # real signature unknown
2         """
3         Remove an element from a set if it is a member.
4
5         If the element is not a member, do nothing.
6         """
7         pass
1 #练习5.移除集合里的单个元素
2 s2 = set(["aaa","bbb","ccc","ddd"])
3 print(s2)
4 s2.discard("aaa")     #移除一个元素,如果元素不存在,do nothing
5 print(s2)
6
7 #执行结果:
8 {‘aaa‘, ‘ccc‘, ‘bbb‘, ‘ddd‘}
9 {‘ccc‘, ‘bbb‘, ‘ddd‘}
 1 def intersection(self, *args, **kwargs): # real signature unknown
 2         """
 3         Return the intersection of two sets as a new set.
 4
 5         (i.e. all elements that are in both sets.)
 6         """
 7         pass
 8
 9     def intersection_update(self, *args, **kwargs): # real signature unknown
10         """ Update a set with the intersection of itself and another. """
11         pass
#练习6.取交集
s2 = set(["aaa","bbb","ccc","ddd"])
print(s2)
s3 = s2.intersection(["aaa"])     #原集合不变,取交集,并生成新集合
print(s3)
s2.intersection_update(["aaa"])    #更新原集合,移除原集合内容,放进交集
print(s2)

执行结果:
{‘ddd‘, ‘bbb‘, ‘ccc‘, ‘aaa‘}
{‘aaa‘}
{‘aaa‘}
1 def isdisjoint(self, *args, **kwargs): # real signature unknown
2         """ Return True if two sets have a null intersection. """
3         pass
#练习6.判断是否有交集
s2 = set(["aaa","bbb","ccc","ddd"])
s3 = s2.isdisjoint(["aaa"])     #有交集则返回False
s4 = s2.isdisjoint(["kkk"])     #没交集则返回True
print(s3)
print(s4)

#执行结果:
False
True
1 def issubset(self, *args, **kwargs): # real signature unknown
2         """ Report whether another set contains this set. """
3         pass
4
5     def issuperset(self, *args, **kwargs): # real signature unknown
6         """ Report whether this set contains another set. """
7         pass
 1 #练习7.判断父子集
 2 s2 = set(["aaa","bbb","ccc","ddd"])
 3 s3 = s2.issubset(["aaa"])     #判断这个集合是否包含原集合,即判断原集合是否子集
 4 s4 = s2.issuperset(["aaa"])     #判断原集合是否包含这个集合,即判断原集合是否父集
 5 print(s3)
 6 print(s4)
 7
 8 #执行结果:
 9 False
10 True
 1 def pop(self, *args, **kwargs): # real signature unknown
 2         """
 3         Remove and return an arbitrary set element.
 4         Raises KeyError if the set is empty.
 5         """
 6         pass
 7
 8     def remove(self, *args, **kwargs): # real signature unknown
 9         """
10         Remove an element from a set; it must be a member.
11
12         If the element is not a member, raise a KeyError.
13         """
14         pass
 1 #练习8.随机删除和指定删除
 2 s2 = set(["aaa", "bbb", "ccc", "ddd"])
 3 s3 = s2.pop()  # 随机删除集合中的一个元素
 4 print(s2)
 5 s2 = set(["aaa", "bbb", "ccc", "ddd"])
 6 s4 = s2.remove("aaa")  # 指定删除集合中的一个元素
 7 print(s2)
 8
 9 #执行结果:
10 {‘ccc‘, ‘ddd‘, ‘aaa‘}
11 {‘bbb‘, ‘ccc‘, ‘ddd‘}
 1     def symmetric_difference(self, *args, **kwargs): # real signature unknown
 2         """
 3         Return the symmetric difference of two sets as a new set.
 4
 5         (i.e. all elements that are in exactly one of the sets.)
 6         """
 7         pass
 8
 9     def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
10         """ Update a set with the symmetric difference of itself and another. """
11         pass
 1 #练习9.差集
 2 s2 = set(["aaa", "bbb", "ccc", "ddd"])
 3 s3 = s2.symmetric_difference(["aaa"])  # 差集,并生成新集合
 4 print(s3)
 5 s4 = s2.symmetric_difference_update(["bbb"])  # 差集,在原集合中改变
 6 print(s2)
 7
 8 #执行结果:
 9 {‘ccc‘, ‘bbb‘, ‘ddd‘}
10 {‘aaa‘, ‘ccc‘, ‘ddd‘}
 1     def union(self, *args, **kwargs): # real signature unknown
 2         """
 3         Return the union of sets as a new set.
 4
 5         (i.e. all elements that are in either set.)
 6         """
 7         pass
 8
 9     def update(self, *args, **kwargs): # real signature unknown
10         """ Update a set with the union of itself and others. """
11         pass
 1 #练习10.并集和更新
 2 s2 = set(["aaa", "bbb", "ccc", "ddd"])
 3 s3 = s2.union(["aaa","kkk"])  # 并集,并生成新集合
 4 print(s3)
 5 s4 = s2.update(["bbb","jjj"])  # 更新,在原集合中改变
 6 print(s2)
 7
 8 #执行结果:
 9 {‘ccc‘, ‘aaa‘, ‘bbb‘, ‘ddd‘, ‘kkk‘}
10 {‘ccc‘, ‘aaa‘, ‘bbb‘, ‘ddd‘, ‘jjj‘}
 
时间: 2024-08-01 06:36:32

Python的set集合浅析的相关文章

Python中set集合的整理

set集合函数主要用来去除重复: 比如一个列表有多个重复值,可以用set搞掉 >>> l = [1,2,3,4,5,4,3,21] >>> >>> l [1, 2, 3, 4, 5, 4, 3, 21] >>> >>> >>> set(l) set([1, 2, 3, 4, 5, 21]) >>> set  可以做交集,并集,差集 set的增删改 增:>>> a

Python字典和集合

1. 字典字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必须是可哈希的.可哈希表示key必须是不可变类型,如:数字.字符串.只含不可变类型元素的元组(1,2,3,'abc').实现__hash__()方法的自定义对象(因为__hash__()须返回一个整数,否则会出现异常:TypeError: an integer is required).可以用ha

Python序列结构--集合

集合:元素之间不允许重复 集合属于Python无序可变序列,元素之间不允许重复 集合对象的创建与删除 直接将值赋值给变量即可创建一个集合 >>> a = {3,5}>>> type(a)<class 'set'> set()函数将列表.元组.字符串.range对象等其他可迭代对象转换为集合,如果原来的数据中存在重复元素,则转换为集合的时候只保留一个:如果原序列或迭代对象中有不可哈希的值,无法转换为集合,抛出异常 >>> a_set=set(

Python的set集合详解

Python 还包含了一个数据类型 —— set (集合).集合是一个无序不重复元素的集.基本功能包括关系测试和消除重复元素.集合对象还支持 union(联合),intersection(交),difference(差)和 sysmmetric difference(对称差集)等数学运算. 创建集合set 大括号或 set() 函数可以用来创建集合. set集合类需要的参数必须是迭代器类型的,如:序列.字典等,然后转换成无序不重复的元素集.由于集合是不重复的,所以可以对字符串.列表.元组进行去重

Python字典、集合结构详解

目录 字典 导言 什么是字典 字典的主要特征 访问字典的值 创建空字典并添加键--值对 修改字典中的值 删除键--值对 遍历字典 遍历所有键--值对 遍历字典中的键 遍历字典中的值 通过映射函数创建字典 集合 导言 什么是集合 set()函数 计算集合元素个数 集合添加.删除元素 添加元素 删除元素 删除.清空集合 删除整个集合 清空集合 集合的交集.并集和差集运算 运算符进行运算 函数实现 参考资料: 例题讲解 四则运算(用字典实现 题目分析 代码实现 列表去重 题目分析: 代码实现 通过两个

Python 元组和集合的特点及常用操作

一.元组的特点: 1.有序的集合 2.通过偏移来取数据 3.属于不可变的对象,不能在原地修改内容,没有排序,修改等操作. tuple支持的方法很少 >>> dir(tuple) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__

python中set集合的使用

集合(set):把不同的元素组成一起形成集合,是python基本的数据类型. python 的集合类型和 其他语言类似, 是一个无序不重复元素集 基本功能包括关系测试和消除重复元素.集合对象还支持union(联合), intersection(交), difference(差)和sysmmetricdifference(对称差集)等数学运算 1.先看下python 集合 类型的不重复性,可以把它转换成集合类型,然后在由集合类型转换成其他的类型. a = [2,3,4,2,1]a = set(a)

14.python中的集合

什么是集合?正如其字面的意思,一堆东西集中合并到一起.乍一听貌似和容器没什么差别,嗯,好吧,集合也算是一种容器. 在学习这个容器有什么不同之前,先看看集合是如何创建的: a = set() #不可变集合 b = frozenset() #可变集合 print a print b 集合分为两种,一种是不可变的,一种是可变的,两者的差异后面会分析. 不过,我们创建了两个空的集合貌似么什么意思. 为了使其有意义,我们就先来看集合最重要的功能:去重. a = ('aaa',123,123,123) b

Awesome Python,Python的框架集合

Awesome Python A curated list of awesome Python frameworks, libraries and software. Inspired by awesome-php. Awesome Python Environment Management 环境管理 Package Management              软件包管理 Package Repositories              软件源 Distribution