Python集合详解

Python内置数据结构之一。

集合的特点

无序,所以没有索引;无重复数据;可多种数据类型混存;可变序列。集合一旦创建,便无法更改元素。

在向集合中添加数据时,它存储的位置是需要通过hash()哈希函数计算得到的,第一个添加的不一定在第一个位置上,所以集合是无序的。

集合的创建

1){}

s = {'hello',99,'python',99,68,'world'}
print(type(s),s)    # <class 'set'> {99, 68, 'world', 'python', 'hello'}

2)内置函数set()

s1 = set((10,20,30,40))
print(type(s1),s1)      # <class 'set'> {40, 10, 20, 30}
s2 = set(range(10))
print(type(s2),s2)      # <class 'set'> {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
s3 = set(['hello',10,'python',21])
print(type(s3),s3)      # <class 'set'> {'hello', 10, 21, 'python'}
s4 = set('python')
print(type(s4),s4)      # <class 'set'> {'o', 'n', 'p', 'h', 't', 'y'}
s5 = set({10,55,77,88})
print(type(s5),s5)      # <class 'set'> {88, 10, 77, 55}

3)创建空集合

只能使用内置函数set() 创建空集合。

s = set()
print(type(s),s)    # <class 'set'> set()
# {}创建的是一个空字典
s1 = {}
print(type(s1),s1)  # <class 'dict'> {}

4)集合生成式

有规则的元素才能用集合生成式。

语法格式:

图片[1]-Python集合详解-尤尤'blog
s = {i*2 for i in range(1,6)}
print(s)    # {2, 4, 6, 8, 10}

s1 = {i*2 for i in 'abc'}   # {'cc', 'aa', 'bb'}
print(s1)

获取集合长度

使用len()方法

s = {10,50,'hello','python',100}
print(len(s))   # 5

集合元素的判断操作

1)in/not in 判断某个元素是否存在

s = {10,50,'hello','python',100}
# 判断10在集合s中是否存在
print(10 in s)  # True
print('p' in s) # False
# 判断100是否不在集合s中
print(100 not in s) # False

集合的遍历

1)使用for-in遍历集合

s = {'hello',99,'python',99,68,'world'}
for i in s:
    print(i)

添加元素

1)add() 一次添加一个元素

s = {10,50,'hello','python',100}
print(id(s),s)  # 1970519003584 {'hello', 'python', 50, 100, 10}
s.add(9999)
print(id(s),s)  # 1970519003584 {'hello', 'python', 50, 100, 10, 9999}
# 列表中的元素不能重复
s.add(10)
print(id(s),s)  # 1970519003584 {'hello', 'python', 50, 100, 10, 9999}

2)update() 一次添加至少一个元素

可以是列表、元组、集合

s = {10,50,'hello','python',100}
s.update({888,'world',123})
print(s)    # {50, 100, 'world', 888, 123, 10, 'hello', 'python'}
# 将字符串拆成一个个字符添加进去
s.update('java')
print(s)    # {'world', 'j', 'a', 'python', 10, 'hello', 100, 50, 'v', 888, 123}

add()和update()的区别:

add()一次只能添加一个元素,update()一次可以添加多个元素,比喻说

s = {10,50,'hello','python',100}
# 把元组看成是一个元素,添加到集合中
s.add((99,22))
print(s)    # {(99, 22), 50, 100, 'python', 10, 'hello'}
s = {10,50,'hello','python',100}
# 将元组中的每一个元素添加到集合中
s.update((99,22))
print(s)    # {50, 99, 100, 'python', 22, 10, 'hello'}

删除元素

1)remove()

一次删除一个指定的元素,如果元素不存在则抛出异常KeyError。

s = {10,50,'hello','python',100}
s.remove(100)
print(s)    # {50, 'hello', 'python', 10}
# 99不存在,无法删除,抛出异常KeyError
# s.remove(99)    # KeyError: 99

2)discard()

一次删除一个指定的元素,如果元素不存在也不会抛出异常。

s = {10,50,'hello','python',100}
s.discard('hello')
print(s)    # {50, 100, 'python', 10}
# 'world'不存在,但不会抛出异常
s.discard('world')

3)pop()

一次删除一个任意元素,因为集合是无序的,所以pop()无法指定参数,是无参的。

s = {10,50,'hello','python',100}
s.pop()    # 无参
print(s)    

4)clear()

清空集合。

s = {10,50,'hello','python',100}
s.clear()
print(s)    # set()

5)del

彻底删除集合对象。

s = {10,50,'hello','python',100}
del s
print(s)    # NameError: name 's' is not defined. Did you mean: 's1'?

集合的其他操作

1)两个集合是否相等==或!=

s1 = {10,20,30,40}
s2 = {20,30,10,40}
print(s1 == s2)     # True
print(s1 != s2)     # False

2)issubset()

判断set1是否是set2的子集。如果是返回True,否则返回False。

语法:set1.issubset(set2)

s1 = {20,40,30,10}
s2 = {10,20,30,40,50,60}
s3 = {10,40,90}
print(s1.issubset(s2))  # True
# 90不在s2里
print(s3.issubset(s2))  # False

3)issuperset()

判断set1是否是set2的超集,也就是说集合set1是否包含集合set2中所有元素。

语法:set1.issuperset(set2)

s1 = {10,20,30,40,50,60}
s2 = {20,40,30,10}
s3 = {10,40,90}
print(s1.issuperset(s2))    # True
# s1不包含s3中的90
print(s1.issuperset(s3))    # False

4)isdisjoint()

判断两个集合是否有交集(有相同元素),如果没有交集返回True,有交集返回False。

语法:set1.isdisjoint(set2)

s1 = {10,20,30,40,50,60}
s3 = {10,40,90}
s4 = {22,99,33,44}
# 有交集{10,40},返回False
print(s1.isdisjoint(s3))    # False
# 没有交集,返回True
print(s1.isdisjoint(s4))    # True

5)intersection()

返回两个集合的交集,也就是两个集合中相同元素组成的集合。得到新集合,原集合不发生改变。

语法:set1.intersection(set2)

s1 = {10,20,30,40,50,60}
s2 = {20,40,88,99,30,10}
print(s1.intersection(s2))  # {40, 10, 20, 30}

6)intersection_update()

取两个集合的交集,和intersection不同的是,intersection得到的是一个新的集合,而intersection_update()是在原集合set1的基础上去掉和set2不重叠的元素。在元集合的基础上操作。

语法:set1.intersection_update(set2)

s1 = {10,20,30,40,50,60}
s2 = {20,40,88,99,30,10}
# 取交集,在集合s1的基础上去除与s2不重叠的元素,留下重复的元素
s1.intersection_update(s2)
print(s1)  # {40, 10, 20, 30}

7)union()

返回两个集合的所有元素组成的集合,称为并集。重复的元素只取一次。相当于|。

语法:set1.union(set2)

s1 = {10,40,70,30}
s2 = {20,30,50,60}

# 合并两个集合
s3 = s1.union(s2)
print(s3)   # {70, 40, 10, 50, 20, 60, 30}

# 取三个集合的并集
s = {88,10,90,666}
print(s1.union(s,s2))   # {70, 40, 10, 50, 20, 666, 88, 90, 60, 30}

# 合并列表
s4 = ['hello','python']
print(s1.union(s4)) # {70, 40, 10, 'python', 'hello', 30}

8)difference()

返回集合的差集,即集合set1减去两个集合的交集,就是set1-set2 。得到的那个集合是第一个集合除去和第二个集合重叠的部分所剩下的元素。相当于- 。原集合不发生改变,返回一个删除相同元素后的新集合。

语法:set1.difference(set2)

s1 = {10,20,30,40,50,60,70}
s2 = {20,33,40,50,88}
# 相当于s1 - s2,得到新集合
print(s1.difference(s2))    # {10, 60, 70, 30}

9)difference_update()

删除两个集合都存在的元素,直接在原来的集合中删除两个集合重复的元素,无返回值。

语法:set1.difference_update(set2)

s1 = {10,20,30,40,50,60,70}
s2 = {20,33,40,50,88}
# 无返回值,直接在s1上操作的
s1.difference_update(s2)
print(s1)       # {70, 10, 60, 30}

10)symmetric_difference()

返回两个集合的对称差集(两个集合的并集减去交集,得到对称差集。),得到的这个新集合是两个集合中不重复的元素组成的,就是除去了两个新集合重复的元素。有返回值。

语法:set1.symmetric_difference(set2)

s1 = {10,20,30,40,50,60,70}
s2 = {20,33,40,50,88}

s3 = s1.symmetric_difference(s2)

print(s3)   # {33, 70, 10, 88, 60, 30}

11)symmetric_difference_update()

删除set1集合中,和set2集合重复的元素,并将set2集合中不同的元素插入到当前集合set1中。无返回值。

语法:set1.symmetric_difference_update(set2)

s1 = {10,20,30,40,50,60,70}
s2 = {20,33,40,50,88}

s1.symmetric_difference_update(s2)

print(s1)   # {33, 70, 88, 10, 60, 30}
© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容