python--集合

集合中的元素都是独一无二且无序的。

创建集合

>>> {'FishC','Python'}#直接大括号创建
{'FishC', 'Python'}
>>> {a for a in 'FishC'}#利用推导式创建集合
{'C', 'h', 's', 'i', 'F'}
>>> set('FishC')#利用类型构造器创建集合
{'C', 'h', 's', 'i', 'F'}

访问集合内元素

集合是无序的,无法通过下标访问。

>>> s = set('FishC')
>>> s[0]
Traceback (most recent call last):
  File "<pyshell#109>", line 1, in <module>
    s[0]
TypeError: 'set' object is not subscriptable
>>> 'C' in s
True
>>> 'c' not in s
True
>>> for each in s:
	print(each)

	
C
h
s
i
F

唯一性

利用集合的唯一性给列表去重。

>>> set([1,1,2,3,5])
{1, 2, 3, 5}

验证列表内是否有重复的元素。

>>> s = [1,1,2,4,5]
>>> len(s) == len(set(s))
False

集合的用法

集合的浅拷贝

>>> s
{1, 2, 4, 5}
>>> t = s.copy()
>>> t
{1, 2, 4, 5}

isdisjoin()函数:没有一样的元素返回True,有一样元素的则返回False。

>>> s = set('FishC')
>>> s
{'C', 'h', 's', 'i', 'F'}
>>> s.isdisjoint(set('python'))
False
>>> s.isdisjoint(set('JAVA'))
True
>>> s.isdisjoint('JAVA')#只要是可迭代对象就行,不一定非要转为集合
True
>>> s.isdisjoint('python')

issubset():如果是该可迭代对象的子集就返回True。

>>> s.issubset('FishC.com.cn')
True

issuperset():如果是该可迭代对象的超集就返回True。

>>> s.issuperset('Fish')
True

并集

>>> s.union({1,2,3})
{'i', 1, 2, 3, 'C', 'h', 'F', 's'}
>>> s.union({1,2,3},'python')
{1, 2, 3, 'h', 'y', 'n', 's', 't', 'F', 'p', 'C', 'i', 'o'}

交集

>>> s.intersection('Fish')
{'i', 'h', 'F', 's'}
>>> s.intersection('Php','python')
{'h'}

差集

>>> s.difference('Fish')
{'C'}
>>> s.difference('Php','Python')
{'i', 'C', 'F', 's'}

对称差集
集合A与集合B中所有不属于A∩B的元素的集合。

>>> s.symmetric_difference('Python')
{'P', 'y', 'C', 'n', 's', 'i', 't', 'o', 'F'}

集合的运算符用法
运算符两边必须是集合。

>>> s <= set('FishC')  #检测是否是子集
True
>>> s < set('FishC')#检测是否是真子集
False
>>> s < set('FishC.com.cn')#检测是否是真子集
True
>>> s > set('FishC')#检测是否是真超集
False
>>> s >= set('FishC')#检测是否是超集
True
>>> s > set('Fish')#检测是否是超集
True
>>> s | {1,2,3} | set('Pyton')#并集
{1, 2, 3, 'P', 'C', 'h', 'y', 'n', 's', 'i', 't', 'o', 'F'}
>>> s & set('Php') & set('Python')#交集
{'h'}
>>> s - set('Php') - set('Python')#差集
{'i', 'C', 'F', 's'}
>>> s ^ set('Python')#对称差集
{'y', 'n', 's', 't', 'F', 'P', 'C', 'i', 'o'}

如果加一个update就会改变集合的内容,如果不加只是输出想要的集合并不会改变s的内容。
由于并集比较常用,python把union_update简化成update直接用了。

>>> s = set('FishC')
>>> s
{'C', 'h', 's', 'i', 'F'}
>>> s.update([1,1],'23')#迭代传入s集合,并集
>>> s
{1, 'C', 'h', '3', 's', 'i', '2', 'F'}
>>> s.intersection_update('FishC')#交集
>>> s
{'C', 'h', 's', 'i', 'F'}
>>> s.difference_update('Php','Python')#差集
>>> s
{'C', 's', 'i', 'F'}
>>> s.symmetric_difference_update('Python')#对称差集
>>> s
{'P', 'C', 'h', 'y', 'n', 's', 'i', 't', 'o', 'F'}
>>> s.add('45')#相较于update的迭代传入,add()是传入整个字符串。
>>> s
{'P', 'C', 'h', 'y', '45', 'n', 's', 'i', 't', 'o', 'F'}

remove():如果删除的元素不存在,会抛出异常。
discard():如果删除的元素不存在,会静默处理。

>>> s = set('FishC')
>>> s
{'C', 'h', 's', 'i', 'F'}
>>> s.remove('狗')
Traceback (most recent call last):
  File "<pyshell#176>", line 1, in <module>
    s.remove('狗')
KeyError: '狗'
>>> s.discard('狗')
>>> s
{'C', 'h', 's', 'i', 'F'}

pop():随机删除一个元素并回显。

>>> s.pop()
'C'
>>> s.pop()
'h'
>>> s
{'s', 'i', 'F'}

clear():清空集合。

>>> s.clear()
>>> s
set()

不可变集合

python将集合细分为可变和不可变对象,分别是set()和frozenset()。

>>> t = frozenset('FishC')
>>> t
frozenset({'C', 'h', 's', 'i', 'F'})

可哈希

想要正确的创建集合和字典,字典的键与集合的元素必须是可哈希的。
如果一个对象是可哈希的,那么它的哈希值必须在其整个程序的生命周期中保持不变。
hash()函数可以获取一个对象的哈希值。

>>> hash(1)#整数的哈希值是它本身
1
>>> hash(1.0)#虽然对象不同,但值等于1,值相等所以哈希值也一样
1
>>> hash(1.001)
2305843009213441

python中大多数不可变的对象都是可哈希的,哈希值保持不变。(可变对象不可哈希)

>>> hash('FishC')#字符串可哈希
1086488666003076458
>>> hash([1,2,3])#列表不可哈希
Traceback (most recent call last):
  File "<pyshell#190>", line 1, in <module>
    hash([1,2,3])
TypeError: unhashable type: 'list'
>>> hash((1,2,3))#元组可哈希
529344067295497451
>>> {'Python':520,'FishC':1314}
{'Python': 520, 'FishC': 1314}
>>> {[1,2,3]:'FishC'}#列表不可作为字典的键
Traceback (most recent call last):
  File "<pyshell#193>", line 1, in <module>
    {[1,2,3]:'FishC'}
TypeError: unhashable type: 'list'
>>> {'Python','FishC',520,1314}
{520, 1314, 'FishC', 'Python'}
>>> {'Python','FishC',520,1314,[1,2,3]}#列表不可作为集合的元素
Traceback (most recent call last):
  File "<pyshell#195>", line 1, in <module>
    {'Python','FishC',520,1314,[1,2,3]}
TypeError: unhashable type: 'list'
>>> x = {1,2,3}
>>> y = {x,4,5}#集合不可直接嵌套,因为集合是一个可变容器,而集合的元素必须是可哈希的不可变对象。
Traceback (most recent call last):
  File "<pyshell#197>", line 1, in <module>
    y = {x,4,5}
TypeError: unhashable type: 'set'
>>> x = frozenset(x)#不可变集合frozenset()可嵌套
>>> y = {x,4,5}
>>> y
{frozenset({1, 2, 3}), 4, 5}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GG_Bomd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值