set
Collections系列:
Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几个额外的数据类型:
一、计数器:
Counter 是对字典的加工处理,功能是计算序列中元素出现的次数。
1 import collections 2 3 import collections 4 obj = collections.Counter('testttteseess') 5 print (obj) 6 > Counter({'t': 5, 's': 4, 'e': 4}) # 统计各字符出现次数,并生成伪字典,key 是传入的元素,value 为元素出现的次数 7 8 obj = collections.Counter('testttteseess') 9 print (obj.most_common(5)) # 统计出现频率最高的5个字符,返回其值 10 > [('t', 5), ('e', 4), ('s', 4)]
elements enumerate
二、有序字典(orderedDict):
对字典功能的一个补充,在字典原有方法上,补充了一些方法,这些方法封装在 collections 中的 orderedDict 中。
默认使用 dict 时,key是无序的,在对dict 做迭代时,无法确定key 的顺序。
OrderedDict 的 key 会按照插入的顺序排列,而不是 key 本身排列。
内部原理:将字典的 key 放进有序的列表,再拿列表中的 key 去取字典中对应的 value。
1 # Python 内部即靠这种原理实现的字典有序 2 3 dic = {'name':'jay','sex':'boy','age':'123','xxx':'123'} 4 for i in dic: # 每循环一次取出一个 key ,根据key 去字典中取对应值 5 print (dic[i])
1 import collections 2 dic = collections.OrderedDict() 3 dic['k1'] = 'v1' 4 dic['k3'] = 'v3' 5 dic['k2'] = 'v2' 6 print (dic()) # 无论取多少次,输出的顺序不会变。 7 > OrderedDict([('k1', 'v1'), ('k3', 'v3'), ('k2', 'v2')]) 8 9 # for 循环取出所有 key 与对应 value 10 for k,v in dic.items(): 11 print (k,v) 12 13 > k1 v1 14 > k3 v3 15 > k2 v2
move_to_end():将指定的 key 移至字典最后位置
1 import collections 2 dic = collections.OrderedDict() 3 dic['k1'] = 'v1' 4 dic['k3'] = 'v3' 5 dic['k2'] = 'v2' 6 dic.move_to_end('k1') # 将 k1 与 k1 对应的值,移至字典最后 7 print (dic) 8 > OrderedDict([('k3', 'v3'), ('k2', 'v2'), ('k1', 'v1')]) # 此时 k1 已经在字典最后
popitem():后进先出,取出字典中最后一个 key 与其对应的 value
1 import collections 2 dic = collections.OrderedDict() 3 dic['k1'] = 'v1' 4 dic['k3'] = 'v3' 5 dic['k2'] = 'v2' 6 ret = dic.popitem() #移除并将返回值赋值给 ret 变量 7 print (dic) 8 print (ret) 9 > OrderedDict([('k1', 'v1'), ('k3', 'v3')]) # 原字典中内容 10 > ('k2', 'v2') # 移除并返回的内容
pop:移除指定 Key,并返回其 value
1 import collections 2 dic = collections.OrderedDict() 3 dic['k1'] = 'v1' 4 dic['k3'] = 'v3' 5 dic['k2'] = 'v2' 6 ret = dic.pop('k2') # 指定移除 k2 7 print (dic) 8 print (ret) 9 > OrderedDict([('k1', 'v1'), ('k3', 'v3')]) 10 > v2 # 此处返回的是 k2 的值
setdefault(key,value):如果键不存在于字典中,则添加其 key 并将其值设定为默认值
1 import collections 2 dic = collections.OrderedDict() 3 dic['k1'] = 'v1' 4 dic.setdefault('k6','v8') # k6 不存在于字典中,会向字典中添加 k6 5 print (dic)
## 字典循环时,默认循环只输出出字典的 key。。。
三、默认字典(defaultdict):
1 import collections 2 dic = collections.defaultdict(list) 3 dic['k1'].append('jay') 4 print (dic)
四、可命名元组(namedtuple):
对元组的一个扩展,默认元组只能通过 [0]、[2] 这种索引去访问,可以将元组的值命名,访问元组的某个值时,可通过名字访问,x y 轴类似于这种。
1 # 导入模块 2 import collections 3 # 创建类,类名为 MytupleClass: 4 MytupleClass = collections.namedtuple('MytupleClass',['x','y','z']) 5 6 obj = MytupleClass(11,22,33) # obj 就是根据 MytupleClass 类创建的对象 7 print (obj.x) # 通过可命名元组的 name 访问其内容。 8 print (obj.y) # 通过可命名元组的 name 访问其内容。 9 print (obj.z) # 通过可命名元组的 name 访问其内容。 10 > 11 11 > 22 12 > 33
五、队列 Queue:
深浅拷贝:
函数:
定义函数,函数在没有被调用时,不会被 Python 解释器所执行。
1 # 定义函数 2 def hello(): 3 print "hello" 4 print "world" 5 print "hello" 6 7 # 执行函数,当函数被执行时,才会被 Python 解释器所解释 8 hello()
return:
1 def show(): # 定义一个名为 show 的函数。 2 print ("a") 3 if 1 == 2: # 此判断永远不会被满足 4 return [11,22,33] # 当函数执行遇到 return 时,执行 return 代码并退出此函数(return 后的代码不会被执行)。 5 print ("b") 6 7 ret = show() # 执行函数 show 并将返回值赋值给 ret,函数默认返回 None 8 print (ret) # 输出函数返回的内容。 9 > a # 函数 show 输出的内容 a 10 > b # 函数 show 输出的内容 b 11 > None # if 永远为假, return 没有被执行所以保存 show的返回值变量 ret 值为空,打印出 None
return:
1 def show(): # 定义一个名为 show 的函数。 2 print ("a") 3 if 1 == 1: # 此判断永远被满足 4 return [11,22,33] # 当函数执行遇到 return 时,执行 return代码并退出此函数(return 后的代码不会被执行)。 5 print ("b") 6 7 ret = show() # 执行函数 show 并将返回值赋值给 ret ,函数默认返回 None 8 print (ret) # 输出函数返回的内容。 9 > a # 函数 show 输出的内容 a 10 > [11,22,33] # ret 变量保存的 函数 show return 出来的值。
形式参数与实际参数:
无参数:
show () # 函数执行时,() 为空代表无参数
一个参数:
1 def args_arg(arg): # (arg) 为形式参数 2 print (arg) # 打印传递进来的形式参数 3 4 args_arg("test vars") # 执行 args_arg 函数,并传递参数 () 中内容为实际参数 5 > test vars
两个参数:
1 def show(arg_1,arg_2): 2 print (arg_1,arg_2) 3 4 show("hello",456) # 传递的实际参数个数,必须与函数中定义的形式参数个数相等 5 > hello 456