一、集合 1.1:作用 可以包含多个元素,用逗号分割,将不同的值存放在一起做关系运算; 每个元素必须是不可变类型(可hash,可作为字典的key); 集合是无序的,不重复的数据集合
一、集合
1.1:作用
1.2:创建
set1 = set({1,2,'郭海亮'})set2 = {1,2,'郭海亮'}
print(set1,set2)
#输出结果:
{1, 2, '郭海亮'} {1, 2, '郭海亮'}
1.3:增
集合add方法:是把要传入的元素做为一个整个添加到集合中,例如:
>>> a = set('boy')>>> a.add('python')
>>> a
set(['y', 'python', 'b', 'o'])
集合update方法:是把要传入的元素拆分,做为个体传入到集合中,例如:
>>> a = set('boy')>>> a.update('python')
>>> a
set(['b', 'h', 'o', 'n', 'p', 't', 'y'])
1.4:删
remove删除一个元素
set1 = {'梨', '迈巴赫', '苹果', '桔子'}set1.remove('苹果')
print(set1)
#输出结果:
{'迈巴赫', '桔子', '梨'}
pop随机删除一个元素
set1 = {'梨', '迈巴赫', '苹果', '桔子'}set1.pop()
print(set1)
#输出结果:
{'桔子', '苹果', '梨'}
clear清空集合
set1 = {'梨', '迈巴赫', '苹果', '桔子'}set1.clear()
print(set1)
#输出结果:
set()
del删除集合
set1 = {'梨', '迈巴赫', '苹果', '桔子'}del set1
print(set1)
#输出结果:
NameError: name 'set1' is not defined
1.5:查
set1 = {'梨', '迈巴赫', '苹果', '桔子'}for i in set1:
print(i)
#输出结果:
苹果
桔子
迈巴赫
梨
1.6:交集(& 或者 intersection)
set1 = {1,2,3,4,5}set2 = {4,5,6,7,8}
print(set1 & set2)
#输出结果
{4, 5}
print(set1.intersection(set2))
#输出结果
{4, 5}
1.7:并集(| 或者 union)
set1 = {1,2,3,4,5}set2 = {4,5,6,7,8}
print(set1 | set2)
# 输出结果
{1, 2, 3, 4, 5, 6, 7}
print(set2.union(set1))
# 输出结果
{1, 2, 3, 4, 5, 6, 7}
1.8:差集(- 或者 difference)
set1 = {1,2,3,4,5}set2 = {4,5,6,7,8}
print(set1 - set2)
# 输出结果
{1, 2, 3}
print(set1.difference(set2))
# 输出结果
{1, 2, 3}
1.9:反交集(^ 或者 symmetric_difference)
set1 = {1,2,3,4,5}set2 = {4,5,6,7,8}
print(set1 ^ set2)
# 输出结果
{1, 2, 3, 6, 7, 8}
print(set1.symmetric_difference(set2))
# 输出结果
{1, 2, 3, 6, 7, 8}
1.10:子集与超集
set1 = {1,2,3}set2 = {1,2,3,4,5,6}
print(set1 < set2)
print(set1.issubset(set2)) # 这两个相同,都是说明set1是set2子集。
print(set2 > set1)
print(set2.issuperset(set1)) # 这两个相同,都是说明set2是set1超集。
#输出结果:
True
True
True
True
1.11:frozenset不可变集合,让集合变成不可变类型。
s = frozenset('barry')print(s,type(s))
# 输出结果
frozenset({'a', 'y', 'b', 'r'})
1.12:例子
- bingo = {"人","生","苦","短","我","用","python"}
- awful = {"python","要","被","我","所","用"}
1.求bingo和awful的交集
>>> bingo = {"人","生","苦","短","我","用","python"}>>> awful = {"python","要","被","我","所","用"}
>>> print(bingo & awful)
{'我', '用', 'python'}
>>>
2.求bingo和awful的并集
>>> bingo = {"人","生","苦","短","我","用","python"}>>> awful = {"python","要","被","我","所","用"}
>>> print(bingo | awful)
{'用', '所', '生', '人', '我', '要', 'python', '苦', '被', '短'}
>>>
3.求只在bingo中出现的元素集合
>>> bingo = {"人","生","苦","短","我","用","python"}>>> awful = {"python","要","被","我","所","用"}
>>> print(bingo - awful)
{'短', '人', '苦', '生'}
>>>
4.求没有同时存在bingo和awful中的元素集合
>>> bingo = {"人","生","苦","短","我","用","python"}>>> awful = {"python","要","被","我","所","用"}
>>> print(bingo ^ awful)
{'所', '要', '生', '人', '被', '苦', '短'}
>>>
5.解压
test,*_={'包子','饺子','丸子',}print(test)
#输出结果:
包子
二、深浅拷贝
2.1:数字和字符串
对于 数字 和 字符串 而言,赋值、浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址。
import copy# ######### 数字、字符串 #########
n1 = 123
# n1 = "i am alex age 10"
print(id(n1))
# ## 赋值 ##
n2 = n1
print(id(n2))
# ## 浅拷贝 ##
n2 = copy.copy(n1)
print(id(n2))
# ## 深拷贝 ##
n3 = copy.deepcopy(n1)
print(id(n3))
2.1:赋值运算
- 对于赋值运算来说,l1与l2指向的是同一个内存地址,所以他们是完全一样的。
只是创建一个变量,该变量指向原来内存地址,如:
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}n2 = n1l1 = [1,2,3,['barry','alex']]
l2 = l1
l1[0] = 111
print(l1)
#输出结果:
[111, 2, 3, ['barry', 'alex']]
print(l2)
#输出结果:
[111, 2, 3, ['barry', 'alex']]
l1[3][0] = 'wusir'
print(l1)
#输出结果:
[111, 2, 3, ['wusir', 'alex']]
print(l2)
#输出结果:
[111, 2, 3, ['wusir', 'alex']]
2.2:浅拷贝(copy)
- 第一层各自独立,从第二层开始,共用一个内存地址。
- 在内存中,只额外地创建一层数据。
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
n3 = copy.copy(n1)s1 = [1, 2, 3]
s2 = s1 # 共用一个内存地址
s1.append(666)
print(s1, s2)
#输出结果:
[1, 2, 3, 666] [1, 2, 3, 666]s1 = [1, 2, 3]
s2 = s1.copy()
s1.append(666)
print(s1, s2)
#输出结果:
[1, 2, 3, 666] [1, 2, 3]s1 = [1, 2, 3,[11,22]]
s2 = s1.copy()
s1[-1].append(666)
print(s1, s2)
print(id(s1), id(s2))
print(id(s1[-1]), id(s2[-1]))
#输出结果:
[1, 2, 3, [11, 22, 666]] [1, 2, 3, [11, 22, 666]]
39103304 39133768
39104328 39104328
2.3:深拷贝(deepcopy)
- 无论多少层都是完全相互独立的,改变任意一个的任何元素(无论多少层),另一个绝对不改变。
- 在内存中将所有的数据重新创建一份(排除最后一层,即:python内部对字符串和数字的优化)。
n1 = {"k1": "wu", "k2": 123, "k3": ["alex", 456]}
n4 = copy.deepcopy(n1)import copy
s1 = [1, 2, 3,[11,22]]
s2 = copy.deepcopy(s1)
s1.append(666)
print(s1, s2)
#输出结果:
[1, 2, 3, [11, 22], 666] [1, 2, 3, [11, 22]]import copy
s1 = [1, 2, 3,[11,22]]
s2 = copy.deepcopy(s1)
s1[-1].append(666)
print(s1, s2)
#输出结果:
[1, 2, 3, [11, 22, 666]] [1, 2, 3, [11, 22]]
2.4:切片,浅copy
s1 = [1, 2, 3, [11, 22]]s2 = s1[:]
s1.append(666)
#输出结果:
[1, 2, 3, [11, 22], 666] [1, 2, 3, [11, 22]]s1 = [1, 2, 3, [11, 22]]
s2 = s1[:]
s1[-1].append(666)
print(s1, s2)
#输出结果:
[1, 2, 3, [11, 22, 666]] [1, 2, 3, [11, 22, 666]
作者:HaydenGuo
每一个前十年都想不到后十年我会演变成何等模样,可知人生无常,没有什么规律,没有什么必然。
只要我还对新鲜的知识、品格的改进、情感的扩张、有胃口,这日子就是值得度过的。