day7:set和深浅copy
1,判断字符串是不是空格isspace函数
s1 = ' '
s2 = ' ssss'
print(s1.isspace())
print(s2.isspace()) 运行结果:
True
False
2,list和dict不可以边循环边删除,删除其中一个,index就变了
li = ["apple","pear","peach","watermelon",'grape']
# 要求删除里面所有的项
for i in range(len(li)):
del li[i] # 错误原因,不可以边循环,边删除,因为删除每一个之后,index就变了,后面的循环就无效了,dict也是这个原因 运行结果:
Traceback (most recent call last):
File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/7.1.py", line 18, in <module>
del li[i]
IndexError: list assignment index out of range
dict1 = {"name1":"lisa","name2":"lucy","hobby":"piano"} # 删掉name的key
for i in dict1:
if "name" in i:
del dict1[i] 运行结果:
Traceback (most recent call last):
File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/7.1.py", line 25, in <module>
for i in dict1:
RuntimeError: dictionary changed size during iteration
3,如何解决上面的问题呢?利用逆向思维,找出要删除的项之外的项,放到另外一个dict或者list里面,然后在赋值给原来的list即可
dict1 = {"name1":"lisa","name2":"lucy","hobby":"piano"}
for i in dict1:
if "name" not in i:
dict2.setdefault(i,dict1[i]) dict1 = dict2
print(dict1.items()) 运行结果:
dict_items([('hobby', 'piano')])
4,如果不想这样的话,可以单独把要删除的项找出来,组成一个单独的list,然后再删除,只是不要一边循环原来的字典一边从原来的的字典里面删除就行,因为这样会打乱已有的循环动作
dict1 = {"name1":"lisa","name2":"lucy","hobby":"piano"}
li2 = []
for i in dict1:
if "name" in i:
li2.append(i) for i in li2: # 发现没循环的不是原来的dict,是新生成的列表
del dict1[i] print(dict1.items()) 运行结果:
dict_items([('hobby', 'piano')])
5,空字典 { } 空集合set(),由于大括号给字典用了了,空字典[] ,空元祖() ,所有这些空值,bool值是false,元祖只有一个值的时候注意加上一个逗号,不然原来是什么类型,还是什么类型
print(type(1))
print(type(1,)) print(type("sss"))
print(type("sss",)) 运行结果:
<class 'int'>
<class 'int'>
<class 'str'>
<class 'str'>
6,集合是可变的数据类型,它里面的数据类型,必须是不可变的数据类型。不可变,意味着可哈希
set1 = {1,2,3,(4,5),"sss",True}
print(set1) 运行结果:
{1, 2, 3, (4, 5), 'sss'} set2 = {"str",[1,2,3],{"name":"alex"}}
print(set2)
运行结果:
Traceback (most recent call last):
File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/7.2_set.py", line 12, in <module>
set2 = {"str",[1,2,3],{"name":"alex"}}
TypeError: unhashable type: 'list'
7,集合添加元素add函数
set1 = {1,2,3,(4,5),"sss",True}
print(set1)
set1.add("lisa")
print(set1) # 集合是无序的,找一找,看看新加的元素添加到哪儿啦? 运行结果:
{1, 2, 3, (4, 5), 'sss'}
{'lisa', 1, 2, 3, (4, 5), 'sss'}
8,set也可以迭代增加,把增加的元素,按照最小单位拆分添加update
set1 = {1,2,3,(4,5),"sss",True}
print(set1)
set1.add("lisa")
print(set1) # 集合是无序的,找一找,看看新加的元素添加到哪儿啦?
set1.update("bobocddd") # 这儿无序,去重,迭代添加都体现出来了
print(set1) 运行结果:
{1, 2, 3, (4, 5), 'sss'}
{1, 2, 3, (4, 5), 'lisa', 'sss'}
{1, 2, 3, 'b', (4, 5), 'd', 'lisa', 'o', 'c', 'sss'}
9,删除,pop (remove return arbitrary refer to the introduction of this function)
set1 = {1,2,3,(4,5),"sss",True}
print(set1.pop())
print(set1) 运行结果:
1
{2, 3, (4, 5), 'sss'}
10,删除的另外一种方法,remove ,无返回值,但是如果指定的元素不存在的话,程序会崩溃
set1 = {1,2,3,(4,5),"sss",True}
print(set1.remove("sss"))
print(set1) # 无返回值
运行结果:
None
{1, 2, 3, (4, 5)}
set1 = {1,2,3,(4,5),"sss",True}
print(set1.remove("XXX",))
print(set1) 运行结果:
Traceback (most recent call last):
File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/7.2_set.py", line 28, in <module>
print(set1.remove("XXX",))
KeyError: 'XXX'
11,清空集合 clear函数,
12,删除集合 del 函数 ,注意清空完是空集合,del完集合就彻底没了
13,集合也可以遍历,按照for....in....
14,求交集,就是数学概念里面的交集 符号& 或者函数intersection
set1 = {1,2,3,(4,5),"sss",True}
set2 = {3,(4,5),"kkk"}
print(set1&set2)
print(set1.intersection(set2)) 运行结果:
{(4, 5), 3}
{(4, 5), 3}
15,求并集,符号|和函数union
set1 = {1,2,3,(4,5),"sss",True}
set2 = {3,(4,5),"kkk"} print(set1|set2)
print(set1.union(set2)) 运行结果:
{'sss', 1, 2, 3, 'kkk', (4, 5)}
{'sss', 1, 2, 3, 'kkk', (4, 5)}
16,反交集 符号^ 或者函数symmetric_difference
set1 ={1,2,3,4,5,6,7}
set2 ={4,5,6,7,8,9,0}
print(set1^set2)
print(set1.symmetric_difference(set2)) 运行结果:
{0, 1, 2, 3, 8, 9}
{0, 1, 2, 3, 8, 9}
17,set1 独有的,符号-和different函数
set1 ={1,2,3,4,5,6,7}
set2 ={4,5,6,7,8,9,0} print(set1-set2) # set1独有的
print(set2-set1) # set2独有的 print(set1.difference(set2))
print(set2.difference(set1)) 运行结果:
{1, 2, 3}
{0, 8, 9}
{1, 2, 3}
{0, 8, 9}
18,子集与超集合 符号> 和符号<
set1 ={1,2,3,4,5,6,7}
set2 ={1,2,3,4} print(set1>set2) #超集
print(set1.issuperset(set2)) #超集
print(set2<set1) #子集
print(set2.issubset(set1)) #子集 运行结果:
True
True
True
True
19,列表去重,先转化为set再把set转化为列表
li = [1,2,3,4,5,6,6,5,4,3,7,6,5,4]
set1 =set(li)
print(set1)
li=list(set1)
print(li) 运行结果:
{1, 2, 3, 4, 5, 6, 7}
[1, 2, 3, 4, 5, 6, 7]
20,深浅copy 浅copy不会改变孙子里面地址,只是儿子地址变了,深copy完完全全一个新的,函数copy和deepcopy
21,enumerate函数
li = ['lisa', 'mary', 'lucy', 'hh']
for i in enumerate(li):
print(i) 运行结果:
(0, 'lisa')
(1, 'mary')
(2, 'lucy')
(3, 'hh') # 可以指定起始位置,自我感觉用处不是很大:
li = ['lisa', 'mary', 'lucy', 'hh']
for i in enumerate(li,3):
print(i) 运行结果:
(3, 'lisa')
(4, 'mary')
(5, 'lucy')
(6, 'hh')
22,集合变成不可变的,frozenset
set1 ={1,2,3,4,5,6,7}
set1=frozenset(set1) #生成一个新的set,注意要赋值回去
print(set1,type(set1))
set1.add(9) 运行结果:
frozenset({1, 2, 3, 4, 5, 6, 7}) <class 'frozenset'>
File "/Users/guolixiao/PycharmProjects/lisa's_practise/boys/7.2_set.py", line 67, in <module>
set1.add(9)
AttributeError: 'frozenset' object has no attribute 'add'
day7:set和深浅copy的更多相关文章
- python 全栈开发,Day7(元组转换,列表以及字典的坑,集合,关系测试,深浅copy,编码补充)
一.元组转换 数字 tu = (1) tu1 = (1,) print(tu,type(tu)) print(tu1,type(tu1)) 执行输出: 1 <class 'int'>(1, ...
- Python基础学习Day7 基础数据类型的扩展 集合 深浅copy
一.基础数据类型的扩展 1.1GBK ---> UTF - 8 # str --->bytes s1 = '太白' # 字符串是unicode编码 b1 = s1.encode('gbk' ...
- python基础3(元祖、字典、深浅copy、集合、文件处理)
本次内容: 元祖 字典 浅copy和深copy 集合 文件处理 1.1元祖 元祖(tuple)与列表类似,不同之处在于元祖的元素不能修改,元祖使用小括号(),列表使用方括号[].元祖创建很简单,只需要 ...
- python学习笔记三 深浅copy,扩展数据类型(基础篇)
深浅copy以及赋值 对于字符串和数字而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. import copy n1 = #n1 = 'hahahaha' #赋值n2 = n1#浅co ...
- Python 第三篇(下):collections系列、集合(set)、单双队列、深浅copy、内置函数
一.collections系列: collections其实是python的标准库,也就是python的一个内置模块,因此使用之前导入一下collections模块即可,collections在py ...
- Python 基础之函数、深浅copy,set及练习
三元运算符通常在Python里被称为条件表达式,这些表达式基于真(true)/假(not)的条件判断,在Python 2.4以上才有了三元操作. 语法格式: X if C else Y 有了三元表达式 ...
- python基础(三)编码,深浅copy
编码,深浅copy encode,decode在python2中使用的一些迷糊,python3中更容易理解 要理解encode和decode,首先我们要明白编码,字符和二进制的相关概念,简单来说,计算 ...
- Python 集合 深浅copy
一,集合. 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. ...
- 基础数据类型的补充和深浅copy
一:关于str 的操作方法补充 1,s.isspace() 判断字符串是否只由空格组成,是,为True,否则,为False. s = ' ' #只能是以至少一个空格组成的字符串(全空格) prin ...
随机推荐
- 爬虫 需要什么样的 CPU,内存 和带宽
所有的需求都看这个图片吧,这个就是我爬取一个网站所用的服务器和服务器资源的消耗情况.
- Android 全局弹出版本更新 Dialog 思考和解决办法
Android 针对版本更新,需要做全局的弹出(需求:版本更新只需要在 App 内全局弹出就可以),思路是使用 AlertDialog ,然后设置 setType 为 TYPE_ALERT_WINDO ...
- Android websock 应用
websocket 在实际的应用中不仅仅能做聊天应用,还可以利用websocket长连接保持数据的实时更新以及信息的推送. websocket 的实现的关键点 第一个:首先需要引入 java-webs ...
- Android 网络知识必知必会
目录: 网络分层 TCP 和 UDP 区别 TCP 三次握手以及为什么需要三次握手 UDP 四次挥手以及为什么需要四次挥手 socket 开发相关 Http 是什么 Https 是什么以及和 HTTP ...
- 清除win下连接的linux的samba服务缓存 用户名和密码
1:cmd 2:在停止查看共享的情况下执行:net use * /del 删除所有 或根据列表,一个个删除连接: net use 远程连接名称 /del
- 每日英语:Doc, Do I Need A Juice Cleanse?
Some drink only vegetable juice. Others soak in Epsom salts. It's all in the pursuit of ridding the ...
- PyCharm 2018 最新激活方式总结(最新最全最有效!!!)
PyCharm 2018 最新激活方式总结(最新最全最有效!!!) 欲善其事,必先利其器.这里我为大家提供了三种激活方式: 授权服务器激活:适合小白,一步到位,但服务器容易被封 激活码激活:适合小白, ...
- Halcon 之dyn_threshold与threshold区别与用法
相同点:都是为了选择想要的灰度区域 dyn_threshold (OrigImage, MeanImage, SmallRaw, 3, 'light') //动态阈值分割 threshold()// ...
- 图像处理滤波应用(Halcon)
1.增强对比度:halcon算子 equ_histo_image (GrayImage, ImageEquHisto) 2.空间滤波基础 滤波指接受或拒绝一定的频率分量.低通滤波器的最终效果是模糊(平 ...
- 教程:SpagoBI开源商业智能之XML Template 图表模板
SpagoBI offers a variety of widgets' examples realized with the Highcharts library, that can be divi ...