集合操作

集合操作

  • 集合只能包含可哈希的数据,如数字、字符串、元组等不可变类型的数据,内置hash函数异常的对象都不能作为集合的元素,也不能作为字典对象的“键”
#创建集合
a = {3, 5}
type(a)
set
a_set = set(range(10))
a_set
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
{x.strip() for x in ('he', 'she','I')}
{'I', 'he', 'she'}
  • 集合操作
# add方法:已存在则忽略
s = {1 ,2 ,3 }
s.add(3)
s
{1, 2, 3}
#update方法:合并另一个集合中的元素并去重
s.update({3, 4})
s
{1, 2, 3, 4}
#pop方法:随机删除并返回集合中的一个元素,集合为空报错
s.pop()
1
# remove方法:删除指定元素,不存在则报错
s.remove(5)
---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-7-1a3184e6173f> in <module>()
      1 # remove方法:删除指定元素,不存在则报错
----> 2 s.remove(5)


KeyError: 5
# discard方法:删除特定元素,不存在则忽略
s.discard(5)
# clear方法:清空集合
s.clear()
s
set()

集合运算

a_set = set([8, 9,10 , 11, 12, 13])
b_set = {0,1,2,3,7,8}
# 并集
a_set | b_set
{0, 1, 2, 3, 7, 8, 9, 10, 11, 12, 13}
# 并集
a_set.union(b_set)
{0, 1, 2, 3, 7, 8, 9, 10, 11, 12, 13}
# 交集
a_set & b_set
{8}
# 交集
a_set.intersection(b_set)
{8}
# 差集
a_set.difference(b_set)
{9, 10, 11, 12, 13}
# 差集
a_set - b_set
{9, 10, 11, 12, 13}
#对称差集
a_set.symmetric_difference(b_set)
{0, 1, 2, 3, 7, 9, 10, 11, 12, 13}
#对称差集
a_set^b_set
{0, 1, 2, 3, 7, 9, 10, 11, 12, 13}
x = {1, 2, 3}
y = {1, 2, 5}
z = {1, 2, 3, 4}

比较集合(包含)关系

x < y
False
x < z #真子集
True
y < z
False
{1,2,3}<={1,2,3} #子集
True
x.issubset(y) #是否为子集
False
x.issubset(z)
True
{3}.isdisjoint({4}) #交集为空集返回true
True

不可变集合frozenset:不提供add()\remove()等

x = frozenset(range(5))
x.add(1)
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-27-dc6136f08449> in <module>()
      1 x = frozenset(range(5))
----> 2 x.add(1)


AttributeError: 'frozenset' object has no attribute 'add'

集合应用案例

import random
import time
#集合和字典的操作在比较上很高效
x1 = list(range(10000))
x2 = tuple(range(10000))
x3 = set(range(10000))
x4 = dict(zip(range(10000),range(10000)))

r = random.randint(0,9999)

for t in (x4, x3, x2, x1):
    start = time.time()
    for i in range(9999999):
        r in t
    print(type(t),"time used:",time.time() - start)
<class 'dict'> time used: 1.1369783878326416
<class 'set'> time used: 1.1725521087646484
<class 'tuple'> time used: 827.3122997283936
<class 'list'> time used: 847.166533946991
import random
# random.choice从序列中获取一个随机元素。
num = set([random.choice(range(1000)) for i in range(100)])
print("len of num:",len(num))
len of num: 99
import random
#指定分布中选取不重复元素
random.sample(range(10000), 20)
[8365,
 2671,
 1096,
 8639,
 9085,
 4136,
 997,
 2009,
 3091,
 856,
 2506,
 9989,
 6382,
 7776,
 8438,
 2739,
 4971,
 1763,
 7367,
 8515]
# 指定list是否有非法元素,转换为set更高效
import random
lstColor = ('red', 'green', 'blue')
colors = [random.choice(lstColor) for i in range(10000)]
if(set(colors) - set(lstColor)):
    print("error")
set("bcdef")
{'b', 'c', 'd', 'e', 'f'}
random.choice?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值