python六大常用基本数据类型之集合

python内置数据结构set

集set

1.可变
2.无序
3.不重复

set定义

一个空集 set()
set(iterable)放入可迭代元素

>>> s2 = set(range(5))
>>> s2
{0, 1, 2, 3, 4}
>>>
>>> s2 = set(list(range(10)))
>>> s2
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
>>>
>>> s2 = set([1])
>>> s2
{1}
>>> s2 = set([])
>>> s2
set()
>>> s4 = {}#这是定义了一个集合 对不起我集合就是字典的弟弟 
>>> s4
{}
>>> a = (9,10)
>>> s5 = set(a)
>>> s5
{9, 10}
>>> a
(9, 10)
>>> s5 = {9,10}
>>> s5
{9, 10}
>>>
>>> s6 = {(1,2),3,'a'}
>>> s6
{(1, 2), 3, 'a'}
>>> s5 = set(s6)
>>> s5
{(1, 2), 3, 'a'}
>>>
>>> c = dict.fromkeys('abcdef')
>>> c
{'a': None, 'b': None, 'c': None, 'd': None, 'e': None, 'f': None}
>>> d = set(c)
>>> d
{'b', 'c', 'e', 'f', 'd', 'a'}
>>>
>>> s7 = {[1]}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
#######################################集合放入不可变可哈希的
>>> s7 = {b"1",(1,),1}
>>> s7
{1, (1,), b'1'}
>>>

set的元素

1.set里的元素必须可以hash
2.由于set经过hash后变得散列分布 不可以使用索引
3.set可以迭代

set增加

add(elem)

1.增加一个元素到set
2.如果元素存在,什么也不做

update(*others)

1.合并其他元素到set中
2.参数others必须可迭代
3.就地修改 返回?

>>> a = set(range(10))
>>> a
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
>>> b = set(range(5,15))
>>> b
{5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
>>> a.update(b)
>>> a
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
>>>
>>> a.add('a')
>>> a
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 'a'}
>>> a.add('a','b')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: add() takes exactly one argument (2 given)
>>>#add添加* 一个元素 *
>>> c = [1,2,3,4,5]
>>> a.add(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> c = (1,2,3,4,5)
>>> a.add(c)
>>> a
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 'a', (1, 2, 3, 4, 5)}
>>>#add 一个 一整个 可hash元素

set删除

remove(elem)

1.从set中移除一个元素

>>> a.add(c)
>>> a
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 'a', (1, 2, 3, 4, 5)}
>>> a.remove(c)
>>> a
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 'a'}
>>>
>>> a
{0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 'a'}
>>> a.add(c)
>>> a
{0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 'a', (1, 2, 3, 4, 5)}
>>> a.remove(c[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 1
>>> c
(1, 2, 3, 4, 5)
>>> c[0]
1

2.元素不存在,抛出keyError

>>> a.remove(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: (1, 2, 3, 4, 5)
>>>

KeyError? 元素唯一 元素可hash 元素是个去代入hash的key

discard(elem)

1.从set中移除一个元素
2.元素不存在,什么都不做

>>> b.discard(0)#不存在什么也不做
>>> b
{5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
>>> b.discard(9)
>>> b
{5, 6, 7, 8, 10, 11, 12, 13, 14}
>>>

pop() -> item

1.移除并返回任意的元素。 因为集合散列的 不连续数据结构 无序
2.空集返回KeyError异常

>>> b
{5, 6, 7, 8, 10, 11, 12, 13, 14}
>>> b.pop()
5
>>> b.pop()
6
>>> b.pop(13)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: pop() takes no arguments (1 given)
>>> c = set()
>>> c
set()
>>> c.pop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'pop from an empty set'
>>>

clear()

1.移除所有元素
使用需谨慎,内存中数据来之不易,玩好内存。

set修改、查询

修改

1.要么删除元素,要么加入新的元素
修改改了key 等于抹了门牌号 或者开了另一个门牌的门
等于删除与添加

查询

由于是非线性结构,无法索引

遍历

虽然盒子里的苹果没有贴号码不能按顺序取出,但是在给定空间里一直取定数苹果总能取完。

成员运算符

1.in 和 not in 判断元素是否在set中
这里in判断是O(1)的,无需遍历所有,直接计算出内存旅馆中的门牌号,而不是一个房间一个房间去找人。

set和线性结构

1.线性结构的查询时间复杂度是O(n),意味着随着数据的规模的增大而增加耗时
2.set、 dict等结构,内部使用hash值作为key,时间复杂度可以做到O(1),查询时间和数据规模无关
3.可hash
1.数值型int、 float、 complex
2.布尔型True、 False
3.字符串string、 bytes
4.tuple
5.None
6.以上都是不可变类型,是可哈希类型,hashable
4.set的元素必须是可hash的

集合

基本概念

集合概念建议维基百科

集合运算

并集

1.union(其他集合)
返回和多个集合合并后的新的集合
2. | 运算符重载
等同union
3.update(其他集合)
和多个集合合并,就地修改
4 |=
等同update

交集

1.intersection(其他集合)
返回和多个集合的交集
2. &
等同intersection
3.intersection_update(其他集合)
获取和多个集合的交集,就地修改
4. &=
等同intersection_update

差集

1.difference(其他集合)
返回和多个集合的差集
2.-
等同difference
3.difference_update(其他集合)
4.-=
等同difference_update

对称差集

1.symmetric_differece(另一个集合)
返回和另一个集合的差集
2.^等同symmetric_differece
3.symmetric_differece_update(另一个集合)
获取和另一个集合的差集并就地修改
4.^=
等同symmetric_differece_update

集合运算链接

官方文档set


border="0" width="330" height="86" src="//music.163.com/outchain/player?type=2&id=1919367&auto=1&height=66">


床陪伴我们终生,我们生在上面,长在上面,最后将死在上面
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值