Day3
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Day3相关的知识,希望对你有一定的参考价值。
Set集合
set是一个无序且不重复的元素组合
创建set对象:s1 = set ()
add添加一个元素
s1 = set() s1.add(‘aaa‘) s1.add(‘bbb‘) print(s1) ################ {‘aaa‘, ‘bbb‘} ################
clear清空数据
s1.clear() print(s1) ############# set() #############
difference 对比差异,difference生成了一个新的集合,原来集合没变
s1 = set([‘andy‘,‘tony‘,‘eric‘,‘tony‘]) print(s1) s2 = s1.difference([‘eric‘,‘tony‘]) print(s2) ###################### {‘tony‘, ‘andy‘, ‘eric‘} {‘andy‘} ######################
set是不重复的
s1 = set([‘andy‘,‘tony‘,‘eric‘,‘tony‘]) print(s1) ############### {‘andy‘, ‘tony‘, ‘eric‘} ###############
difference_update
s1 = set([‘andy‘,‘tony‘,‘eric‘,‘tony‘]) print(s1) s2 = s1.difference([‘eric‘,‘tony‘]) print(s2) s3 = s1.difference_update([‘eric‘,‘tony‘]) print(s3) ################# {‘eric‘, ‘andy‘, ‘tony‘} {‘andy‘} None #################
Counter计数器
对字典的补充,用于追踪值的出现次数
有序字典
orderdDict是对字典类型的补充,它记住了字典元素添加的顺序
import collections dic = collections.OrderedDict() dic[‘k1‘] = ‘v1‘ dic[‘k2‘] = ‘v2‘ dic[‘k3‘] = ‘v3‘ print(dic) ###################### OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)]) ######################
默认字典
import collections dic = collections.defaultdict(list) print(dic) dic[‘k1‘].append(‘andy‘) print(dic) ################### defaultdict(<class ‘list‘>, {}) defaultdict(<class ‘list‘>, {‘k1‘: [‘andy‘]}) ###################
dic = {‘k1‘:[]} dic[‘k1‘].append(‘andy‘) print(dic) ################ {‘k1‘: [‘andy‘]} ################
可命名元组
import collections tupleclass = collections.namedtuple(‘tupleclass‘,[‘x‘,‘y‘,‘z‘]) obj = tupleclass(11,22,33) print(obj.x) print(obj.y) print(obj.z) ################### 11 22 33 ###################
双向队列
import collections d = collections.deque() d.append(‘1‘) d.append(‘10‘) d.append(‘1‘) print (d) r = d.count(‘1‘) print(r) d.extend([‘yy‘,‘uu‘]) print(d) d.extendleft([‘qq‘,‘zz‘]) print(d) ###################### deque([‘1‘, ‘10‘, ‘1‘]) 2 deque([‘1‘, ‘10‘, ‘1‘, ‘yy‘, ‘uu‘]) deque([‘zz‘, ‘qq‘, ‘1‘, ‘10‘, ‘1‘, ‘yy‘, ‘uu‘]) ######################
单向队列
浅拷贝
浅拷贝id内存值是一样的
import copy a1 = 123 a2 = copy.copy(a1) #浅拷贝 print(a2) print(id(a1)) print(id(a2)) ################ 123 1696454544 1696454544 ################
深拷贝
import copy a1 = 123 a2 = copy.deepcopy(a1) print(a2) print(id(a1)) print(id(a2)) ############## 123 1696454544 1696454544 ##############
深拷贝和浅拷贝对于字符串和数字来说,id内存值是一样一样的
拷贝只能拷贝第一层,所以列表等发生了内存地址变化
import copy n1 = {‘k1‘:‘wu‘,‘k2‘:123,‘k3‘:["alex",456]} n2 = n1 print(id(n1)) print(id(n2)) n3 = copy.copy(n1) print(id(n3)) n4 = copy.deepcopy(n1) print(id(n4)) print(id(n1[‘k3‘])) print(id(n3[‘k3‘])) ################### 40949256 40949256 41387080 40949768 43805704 43805704 ###################
深浅拷贝应用
字典浅拷贝之后原文件的内存值也发生了变化
dic = { ‘cpu‘:[80,], ‘mem‘:[80,], ‘disk‘:[80,] } print (dic) import copy new_dic = copy.copy(dic) print(new_dic) new_dic [‘cpu‘][0] = 50 print(dic) print(new_dic) ####################### {‘cpu‘: [80], ‘disk‘: [80], ‘mem‘: [80]} {‘cpu‘: [80], ‘disk‘: [80], ‘mem‘: [80]} {‘cpu‘: [50], ‘disk‘: [80], ‘mem‘: [80]} {‘cpu‘: [50], ‘disk‘: [80], ‘mem‘: [80]} #######################
使用深拷贝,源文件的内存值则不会发生变化
dic = { ‘cpu‘:[80,], ‘mem‘:[80,], ‘disk‘:[80,] } print (dic) import copy # new_dic = copy.copy(dic) new_dic = copy.deepcopy(dic) print(new_dic) new_dic [‘cpu‘][0] = 50 print(dic) print(new_dic) ######################### {‘mem‘: [80], ‘disk‘: [80], ‘cpu‘: [80]} {‘mem‘: [80], ‘disk‘: [80], ‘cpu‘: [80]} {‘mem‘: [80], ‘disk‘: [80], ‘cpu‘: [80]} {‘mem‘: [80], ‘disk‘: [80], ‘cpu‘: [50]} #########################
函数的基本定义
def mail(): n = 123 n += 1 print(n) mail() f = mail f() ########## 124 124 ##########
函数的返回值return,
如果函数里没有return,默认返回是None
def mail(): n = 123 n += 1 print(n) return "bingo" ret = mail() print (ret) ############## 124 bingo ##############
函数的普通参数
def show(x,y): print(x,y) show(‘aaa‘,‘bbb‘) show("bbb","aaa") ############### aaa bbb bbb aaa ###############
函数的默认参数
def show(x,y=999): print(x,y) show(y=98,x=99) ################ 99 98 ################
默认参数要放在最后
def show(x=111,y): print(x,y) show() ############### def show(x=111,y): ^ SyntaxError: non-default argument follows default argument ###############
函数的动态参数
def show(*arg):#一个*传元组 print(arg,type(arg)) show(1,2,3,4,5,6) ################ (1, 2, 3, 4, 5, 6) <class ‘tuple‘> ################
def show(**arg): #两个*传入是字典 print(arg,type(arg)) show(n1=78,n2=89,n=0) ################## {‘n2‘: 89, ‘n‘: 0, ‘n1‘: 78} <class ‘dict‘> ##################
def show(*arg,**kwargs): print(arg,type(arg)) print(kwargs,type(kwargs)) show(11,22,33,44,n1=88,b=‘ddc‘) ###################### (11, 22, 33, 44) <class ‘tuple‘> {‘b‘: ‘ddc‘, ‘n1‘: 88} <class ‘dict‘> ######################
使用动态参数实现字符串格式化
s1 = "{0} is {1}" print(type(s1)) s2 = s1.format(‘xxx‘,‘yyy‘) print(s2) ############### <class ‘str‘> xxx is yyy ###############
s1 = "{0} is {1}" l = [‘aaa‘,‘bbb‘] s2 = s1.format(*l) print(s2) ############### aaa is bbb ###############
s1 = "{name} is {acter}" s2 = s1.format(name = ‘a‘,acter = ‘db‘) print(s2) #################### a is db ####################
s1 = "{name} is {acter}" # s2 = s1.format(name = ‘alex‘,acter = ‘sb‘) d = {‘name‘:‘ddd‘,‘acter‘:‘qqq‘} s2 = s1.format(**d) print(s2) ################# ddd is qqq #################
lambda表达式
def fuc(a): b = a+1 return b c = fuc(99) print(c) ############## 100 ##############
func = lambda a:a+1 ret = func(99) print(ret) ############# 100 #############
以上是关于Day3的主要内容,如果未能解决你的问题,请参考以下文章