学习笔记(11):零基础搞定 Python 入门到实战-集合

立即学习:https://edu.csdn.net/course/play/26676/338782?utm_source=blogtoedu

集合:是数学概念,满足确定性、无序性、互逆异性

一、可变集合的定义:内置函数set()

1、使用set()函数或‘{}’定义可变集合对象
2、集合中的元素必须是不可变对象(包含元组、字典、不可变集合),所以元素不能是列表、可变集合
3、方法:是可变对象,可以增(add)删(pop,remove,discard)改

>>> dir(s)
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
二、不可变集合的定义:内置函数frozenset()
1、使用frozenset()函数定义不可变集合对象
2、集合对象中的元素必须是不可变对象,所以元素可以是不可变集合
3、方法:是不可变对象,无增删改

>>> dir(f_set)
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'copy', 'difference', 'intersection', 'isdisjoint', 'issubset', 'issuperset', 'symmetric_difference', 'union']
>>>
三、容器类对象(列表、字典、集合)的浅拷贝和深拷贝方法
1、判断2个对象d1、d1是否是同一个对象的方法
1.1、使用ID()函数看内存地址是否相同
1.2、使用is逻辑运算,看返回值True or False
2、浅拷贝:容器类对象(列表、元组、字典、集合)内置的copy()方法,即只对第一层容器生成新容器对象

>>>
>>> b1 = ['name',123,['python','php']]
>>> b2 = copy(b1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'copy' is not defined
>>> b2 = b1.copy()
>>> b2
['name', 123, ['python', 'php']]
>>> id(b1)
1934157061896
>>> id(b2)
1934157061768
>>> b1 is b2
False
>>> b1[0]
'name'
>>> b1[0] is b2[0]
True
>>> b1[2] is b2[2]
True
>>> b1[0] = 100
>>> b1
[100, 123, ['python', 'php']]
>>> b2
['name', 123, ['python', 'php']]
>>> b1[2][0]
'python'
>>> b1[2][0] = 999
>>> b1
[100, 123, [999, 'php']]
>>> b2
['name', 123, [999, 'php']]
>>>
3、深拷贝:引用包copy,使用copy.deepcopy()方法,则新旧对象中嵌套元素的变动不会相互影响,是彻底不同的拷贝。
>>> b1
[100, 123, [999, 'php']]
>>> import copy
>>> b3 = copy.deepcopy(b1)
>>> b3
[100, 123, [999, 'php']]
>>> b1[2][0] = 'java'
>>> b1
[100, 123, ['java', 'php']]
>>> b3
[100, 123, [999, 'php']]
>>>

四、集合的关系和运算
1、包含(in)、超集(issuperset)、子集(issubset)

>>> b1={1,2,3,4,5,6}
>>> b2={1,2,3}
>>> 1 in b1
True
>>> len(b1)
6
>>>
>>> b2={1,2,3}
>>> b1
{1, 2, 3, 4, 5, 6}
>>> b1.issupperset(b2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'set' object has no attribute 'issupperset'
>>> b1.issuperset(b2)
True
>>> b2.issunset(b1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'set' object has no attribute 'issunset'
>>> b2.issubset(b1)
True
>>>
2、集合运算操作:集合并集("|" or union)、集合交集(“&” or intersection)、集合差集(“-”or difference)
>>> a = {1,2,3}
>>> b ={1,2}
>>> a|b
{1, 2, 3}
>>> a union b
  File "<stdin>", line 1
    a union b
          ^
SyntaxError: invalid syntax
>>> a.union(b)
{1, 2, 3}
>>> a&b
{1, 2}
>>> a.intersection(b)
{1, 2}
>>> a-b
{3}
>>> a.diffrence(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'set' object has no attribute 'diffrence'
>>> a.difference(b)
{3}
>>>

五、课程代码
>>> s = set([1,2,3,3,2,1,4])
>>> s
{1, 2, 3, 4}
>>> type(s)
<class 'set'>
>>> s2 = {'python',2,3}
>>> type(s2)
<class 'set'>
>>> s3 = {'python',[1,2,3]}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

>>> s
{1, 2, 3, 4}
>>> s.add('python')
>>> s
{1, 2, 3, 4, 'python'}
>>> s.pop()
1
>>> s.pop()
2
>>> s
{3, 4, 'python'}
>>> s.remove(3)
>>> s
{4, 'python'}
>>> s.discard(3)
>>> s.remove(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 3
>>> s
{4, 'python'}
>>> s.remove(4)
>>> s
{'python'}
>>>

>>> f_set = frozenset({'qiwsir'})
>>> f_set
frozenset({'qiwsir'})
>>> type(f_set)
<class 'frozenset'>
>>> f_set = frozenset('qiwsir')
>>> f_set
frozenset({'i', 'w', 'q', 's', 'r'})
>>>

>>> b1={1,2,3,4,5,6}
>>> b2={1,2,3}
>>> 1 in b1
True
>>> len(b1)
6
>>> b2.add(f_set)
>>> b2
{frozenset({'i', 'w', 'q', 's', 'r'}), 1, 2, 3}
>>> b2.add(b1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
>>> t=('a','c')
>>> type(t)
<class 'tuple'>
>>> b2.add(t)
>>> b2
{1, 2, 3, ('a', 'c'), frozenset({'i', 'w', 'q', 's', 'r'})}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值