Python基础6 —— 集合

本文详细介绍了Python编程中的集合创建、基本操作,包括添加、删除元素,以及集合的并集、交集、差集等运算,同时探讨了基于集合的条件判断应用。
摘要由CSDN通过智能技术生成
集合(set)是一个无序的不重复元素序列
可以使用大括号({})或者set()函数创建集合
注意: 创建一个空集合必须用set()而不是{},因为{}是用来创建一个空字典

集合的创建

>>> # 用{}方式创建集合(可以看出集合是无序的)
>>> animals = {'dog','cat','monkey'}
>>> print(animals)
{'dog', 'monkey', 'cat'}
>>> print (type(animals))
<class 'set'>

>>> # 用set方式创建集合(可以看出集合拥有去重功能)
>>> letter = set('letter')
>>> print (letter)
{'r', 't', 'l', 'e'}
>>> print (type(letter))
<class 'set'>

集合的基本操作

>>> # add()方法,为集合添加元素(若元素存在时不做任何操作)
>>> animals.add('dog')
>>> print (animals)
{'dog', 'monkey', 'cat'}
>>> animals.add('tiger')
>>> print (animals)
{'dog', 'tiger', 'monkey', 'cat'}

>>> # update()方法,为集合添加元素(可以是多个列表、字典、元组等,用逗号分开)
>>> animals.update({'lion','elephant'})
>>> print (animals)
{'tiger', 'cat', 'elephant', 'dog', 'lion', 'monkey'}
>>> animals.update(['mouse','rhinoceros'],['rabbit','horse'])
>>> print (animals)
{'cat', 'lion', 'elephant', 'horse', 'rhinoceros', 'dog', 'rabbit', 'tiger', 'mouse', 'monkey'}

>>> # remove()方法,移除集合中的指定元素(若元素不存在会出现错误)
>>> animals.remove('cat')
>>> print (animals)
{'lion', 'elephant', 'horse', 'rhinoceros', 'dog', 'rabbit', 'tiger', 'mouse', 'monkey'}
>>> animals.remove('duck')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'duck'

>>> # discard()方法,移除集合中的指定元素(元素不存在时不会发生错误)
>>> animals.discard('horse')
>>> print (animals)
{'lion', 'elephant', 'rhinoceros', 'dog', 'rabbit', 'tiger', 'mouse', 'monkey'}
>>> animals.discard('duck')
>>> print (animals)
{'lion', 'elephant', 'rhinoceros', 'dog', 'rabbit', 'tiger', 'mouse', 'monkey'}

>>> # pop()方法,随机移除元素(因为无序所以随机)
>>> animals.pop()
'lion'
>>> print(animals)
{'elephant', 'rhinoceros', 'dog', 'rabbit', 'tiger', 'mouse', 'monkey'}

>>> # copy()方法,复制一个集合
>>> animalsCopy = animals.copy()
>>> print (animalsCopy)
{'mouse', 'tiger', 'rhinoceros', 'monkey', 'elephant', 'rabbit', 'dog'}

>>> # clear()方法,移除集合中的所有元素
>>> animalsCopy.clear()
>>> print (animalsCopy)
set()

>>> # 判断元素是否在集合内
>>> 'tiger' in animals
True
>>> 'duck' in animals
False

>>> # len()方法,获取集合中元素个数
>>> print (len(animals))
7

集合的运算

>>> # 创建测试集合
>>> testFruits1 = {'apple','orange','banana'}
>>> testFruits2 = {'apple','plum','peach'}

>>> # 差集(返回包含在第一个集合且不包含在第二个集合中的所有元素)
>>> print (testFruits1 - testFruits2)
{'banana', 'orange'}
>>> print (testFruits1.difference(testFruits2))
{'banana', 'orange'}

>>> # 并集(返回两个集合中包含的所有元素)
>>> print (testFruits1 | testFruits2)
{'apple', 'banana', 'peach', 'plum', 'orange'}
>>> print (testFruits1.union(testFruits2))
{'apple', 'banana', 'peach', 'plum', 'orange'}

>>> # 交集(返回两个集合中都包含的元素)
>>> print (testFruits1 & testFruits2)
{'apple'}
>>> print (testFruits1.intersection(testFruits2))
{'apple'}

>>> # 补集(返回两个结合中不相同的所有元素)
>>> print (testFruits1 ^ testFruits2)
{'banana', 'plum', 'orange', 'peach'}
>>> print (testFruits1.symmetric_difference(testFruits2))
{'banana', 'plum', 'orange', 'peach'}

>>> # 移除当前集合中在参数集合(testFruits2)内不包含的元素
>>> testFruits1.intersection_update(testFruits2)
>>> print (testFruits1)
{'apple'}

>>> # 移除当前集合中在参数集合(testFruits1)内包含的元素
>>> testFruits2.difference_update(testFruits1)
>>> print (testFruits2)
{'peach', 'plum'}

>>> # 移除参数集合(testFruits2)中包含的元素,并将不包含的元素合并到当前集合中
>>> testFruits1 = {'apple','orange','banana'}
>>> testFruits2 = {'apple','plum','peach'}
>>> testFruits1.symmetric_difference_update(testFruits2)
>>> print (testFruits1)
{'banana', 'plum', 'orange', 'peach'}

集合的条件判断

>>> testFruits1 = {'apple','orange','banana'}
>>> testFruits2 = {'apple','plum','peach'}
>>> testFruits3 = {'plum','peach'}

>>> # isdisjoint()方法,判断两个集合是否包含相同的元素(没有返回True,有返回False)
>>> print (testFruits1.isdisjoint(testFruits2))
False
>>> print (testFruits1.isdisjoint(testFruits3))
True

>>> # issubset()方法,判断指定集合是否为该方法参数集合的子集(是返回True,否返回False)
>>> print (testFruits3.issubset(testFruits2))
True
>>> print (testFruits3.issubset(testFruits1))
False

>>> # issuperset()方法,判断该方法的参数集合是否为指定集合的子集(是返回True,否返回False)
>>> print (testFruits2.issuperset(testFruits3))
True
>>> print (testFruits2.issuperset(testFruits1))
False
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值