Python3基础-集合

时间:2019-10-14
本文章向大家介绍Python3基础-集合,主要包括Python3基础-集合使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

集合

  • 1、不同元素组成的集合
  • 2、集合是一组无需排序的可hash值
  • s={(1,2,3),1}
    print(s)  #无序的 输出 {1, (1, 2, 3)}
  • 3、集合中的元素必须是不可变类型
#集合
s={'a','a','a',1,2,3,'b',(1,2,'a')}
print(s)  #去重‘a’

a_in='a' in s #快速判断元素是否在集合内
print(a_in)

aa_in='aa' in s #快速判断元素是否在集合内
print(aa_in)

#s={'a',1,2,3,'b',(1,2,'a'),{'a':'aaa'}} # 集合里面不允许有字典 TypeError: unhashable type: 'dict'
#s={'a',1,2,3,'b',(1,2,'a'),[1,2,3,'a']} #集合里面不允许有 列表  TypeError: unhashable type: 'list'

集合的创建

#定义可变集合set
set_test=set('hello world')
print(set_test) #{'w', 'e', 'o', 'h', 'l', 'r', 'd', ' '}

set_test1=set(['aa','abex',1,2,(1,2),(1,2)])
print(set_test1)  #输出  {'abex', 1, 2, (1, 2), 'aa'}

#定义不可变集合frozenset
f_set_test=frozenset(set_test)
print(f_set_test) #输出   frozenset({'w', 'e', 'o', 'h', 'l', 'r', 'd', ' '})

集合的基本操作

1、添加元素

s={'a','b','c','d','e'}
s.add('1')
print(s)  #{'a', 'b', 'c', 'e', '1', 'd'}
s.add('1')  #重复添加
print(s)  #{'a', 'b', 'c', 'e', '1', 'd'}
s.add(1)
print(s) #{1, 'a', '1', 'c', 'd', 'e', 'b'}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
s1 = {1,2}
s2 = {1,2,3}
s1.update(s2)
print(s1)

s1 = {1,2}
s2 = {4,5,6}
s1.update(s2)  #更新多个值
print(s1)

s1.update(['a','b','c'])
print(s1)

s1.add(7)  #只能更新一个值
print(s1)

#s1.add(8,9)  #更新多个值,报错TypeError: add() takes exactly one argument (2 given)

"""
虚线下print执行结果
{1, 2, 3}
{1, 2, 4, 5, 6}
{1, 2, 4, 5, 6, 'a', 'b', 'c'}
{1, 2, 4, 5, 6, 'c', 7, 'b', 'a'}
"""

2、清空、拷贝、删除元素

s.clear()  #清空集合
print(s)  #输出set()
s={1, 'a', '1', 'c', 'd', 'e', 'b'}
s1=s.copy() #拷贝集合
print(s1)  #{1, 'c', 'a', 'd', 'b', '1', 'e'}
s1.pop() #随机删除一个元素
print(s1) #输出 {'e', 'a', 'c', 'b', '1', 'd'}

s={1, 'a', '1', 'c', 'd', 'e', 'b'}
s.remove('a') #指定删除存在元素 ‘a’
print(s)

s.remove('aa') #指定删除不存在元素 ‘aa’ 
print(s) #则会报错 KeyError: 'aa'

s.discard('aa') #指定删除不存在元素 ‘aa’,不会报错
print(s) #直接输出集合,不会报错

s.discard('b') #指定删除不存在元素 ‘b,不会报错
print(s) #输出 {1, '1', 'd', 'c', 'e'}

3、集合关系运算交、差、并集

a=set('abcdefy')
b=set('abcdeio')
print(a,b)

#求差集
s1=a-b   #集合中a中包含而基本b中不包含的元素
print("a-b",s1)
print("差集",a.difference(b))

s2=b-a   #集合中a中包含而基本b中不包含的元素
print("b-a",s2)
print("差集",b.difference(a))

#求并集
s3 = a|b    #集合a或者b 包含的所有元素
print("a|b",s3)
print("并集",a.union(b))

#求交集
s4 = a & b #集合a 和集合 b 都包含的元素
print("a&b",s4)
print("交集",a.intersection(b))

#交叉补集
s5 = a^b  #不包含a和b的元素
print("a^b ",s5)
print("交叉补集",a.symmetric_difference(b))

"""
执行结果如下
{'d', 'e', 'a', 'b', 'y', 'c', 'f'} {'d', 'e', 'a', 'b', 'c', 'o', 'i'}
a-b {'y', 'f'}
差集 {'y', 'f'}
b-a {'o', 'i'}
差集 {'o', 'i'}
a|b {'d', 'e', 'a', 'b', 'y', 'c', 'f', 'o', 'i'}
并集 {'d', 'e', 'a', 'b', 'y', 'c', 'f', 'o', 'i'}
a&b {'d', 'e', 'a', 'b', 'c'}
交集 {'d', 'e', 'a', 'b', 'c'}
a^b  {'y', 'f', 'o', 'i'}
交叉补集 {'y', 'f', 'o', 'i'}
"""
a=set('abcdefy')
b=set('abcdeio')
z = a.isdisjoint(b)  #如果有包含则返回 False
print(z)

x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "facebook"}
z = x.isdisjoint(y)  # 如果没有包含则返回 True
print(z)

s1 = {1,2}
s2 = {1,2,3}
print(s1.issubset(s2))  #s1 是s2的子集  则True
print(s2.issubset(s1))  #s2 不是s2的子集  则False

print(s2.issuperset(s1)) #s2 是s1的父集 则True
print(s1.issuperset(s2)) #s1 是s2的父集 则False

"""
执行结果
False
True
True
False
True
False
"""

4、difference_update()

方法用于移除两个集合中都存在的元素
difference_update() 方法与 difference() 方法的区别在于 difference() 方法返回一个移除相同元素的新集合,而 difference_update() 方法是直接在原来的集合中移除元素,没有返回值。
a=set('abcdefy')
b=set('abcdeio')
print(a,b)
a.difference_update(b)
print(a)

a=set('abcdefy')
b=set('abcdeio')
b.difference_update(a)
print(b)

"""
执行结果
{'d', 'a', 'c', 'b', 'y', 'f', 'e'} {'i', 'o', 'd', 'a', 'c', 'b', 'e'}
{'y', 'f'}
{'i', 'o'}
"""

 5、集合转换成列表、字符串

s={1,2,3,5,6}
lists=list(s)  #集合转换成列表
print(lists)

s={1,'a','b'}
strs=str(s)   #集合转换成字符串
print(strs)

s={'c','a','b'}
strs=''.join(s)  #集合全部包含字符串,则用join
print(strs)

原文地址:https://www.cnblogs.com/sugh/p/11672448.html