03 2017 档案

摘要:import re # 运算字典 calc = { '+': lambda x, y: x + y, # 加 '-': lambda x, y: x - y, # 减 '*': lambda x, y: x * y, # 乘 '/': lambda x, y: x / y, # 除 '%': lambda x, y: x % y, # 取余数 ... 阅读全文
posted @ 2017-03-12 21:19 破斧呈粥 阅读(352) 评论(0) 推荐(0) 编辑
摘要:#slist = [[i for i in range(4)] for l in range(8)] # slist = [ # [4, 5, 2, 7], # [8, 7, 4, 2], # [1, 9, 7, 3], # [0, 4, 5, 6], # ] # 4*4 slist = [ [4, 5, 2, 7], [8, 7, 4, 2... 阅读全文
posted @ 2017-03-12 18:41 破斧呈粥 阅读(203) 评论(0) 推荐(0) 编辑
摘要:# 在有序序列内查找,判断大于还是小于中间值,每次搜索减一半,直到达到退出条件 # 递归二分查找具体索引 def search(num, start, end, args): global m m += 1 middle = (start + end) // 2 # 整除取中间值 # print('start:%s , end:%s , middle:%s ... 阅读全文
posted @ 2017-03-12 18:37 破斧呈粥 阅读(155) 评论(0) 推荐(0) 编辑
摘要:日志格式 阅读全文
posted @ 2017-03-12 18:33 破斧呈粥 阅读(102) 评论(0) 推荐(0) 编辑
摘要:日志格式 backend test.oldboy.orgb1 backend buy.oldboy.org 阅读全文
posted @ 2017-03-12 18:28 破斧呈粥 阅读(102) 评论(0) 推荐(0) 编辑
摘要:''' # 不改变原函数代码,在调用原函数之前或之后执行其他代码 # 不加参数是两层def,会直接将原函数地址传递进来,返回new_fun(带原函数地址)下的inner函数 # 原函数执行前调用的函数(统一参数) # def new_fun(sfunc): # def inner(args): # # 新添加内容 # print('装饰符...') # ... 阅读全文
posted @ 2017-03-12 18:24 破斧呈粥 阅读(255) 评论(0) 推荐(0) 编辑
摘要:import re s = '3456fpeiuHa123@126.com' # 从开头匹配 ret = re.match('34', s) print(ret) # 找第一个 ret = re.search('56', s) print(ret) #替换 ret= re.sub('@','|',s) print(ret) # 匹配一个数字 ret = re.match('[0-9]'... 阅读全文
posted @ 2017-03-12 18:23 破斧呈粥 阅读(143) 评论(0) 推荐(0) 编辑
摘要:import time # 迭代器:通过next向后一次一次取值,不能向前,不能通过下标取值 tup = iter((1, 2, 3, 4, 5, 6)) lis = iter([11, 22, 33, 44, 55]) # 循环一次,相当于执行了一次__next__ for i in lis: print(i) # 生成器 def ggg(generator): whi... 阅读全文
posted @ 2017-03-12 18:21 破斧呈粥 阅读(115) 评论(0) 推荐(0) 编辑
摘要:#函数体内按条件,调用自己 #递归相除 def sal(x): n = x / 2 if n > 1: sal(n) print(n) sal(1000) # 斐波那契数 def fun1(a, b, lis=[]): if a 0: num -= 1 fun2(num) fun2(10) print(su... 阅读全文
posted @ 2017-03-12 18:20 破斧呈粥 阅读(168) 评论(0) 推荐(0) 编辑
摘要:import collections as con st = '1324243598234598756' tup = (4,67,7,5,6,67,78,8,4,2,2,4, 4,56,7,8,5,3,23,45,468,9,1,) lis = [1,4,5,7,8,2,2,1,56,7,89,34,2,3,4,5,6,5,8,7,4,12,1] # 计算序列某元素出现的次数 ... 阅读全文
posted @ 2017-03-12 18:19 破斧呈粥 阅读(161) 评论(0) 推荐(0) 编辑
摘要:import collections as con # 有序添加和取字典元素 ord = con.OrderedDict() ord['a'] = 1 ord['b'] = 2 ord['c'] = 3 print(ord, 'ordereddict') # 移动某元素到最后 ord.move_to_end('a') print(ord, 'move_to_end') 阅读全文
posted @ 2017-03-12 18:18 破斧呈粥 阅读(152) 评论(0) 推荐(0) 编辑
摘要:import collections as con #默认字典数据类型(参数填数据类型) dic = con.defaultdict(list) dic['a'].append(55) print(dic,'defaultdict') 阅读全文
posted @ 2017-03-12 18:17 破斧呈粥 阅读(150) 评论(0) 推荐(0) 编辑
摘要:import collections as con #用namedtyuple创建一个类 classtuple = con.namedtuple('classtuple', ['x', 'y', 'z']) #用这个类创建可命名元组对象 namet= classtuple(1,2,3) print 阅读全文
posted @ 2017-03-12 18:15 破斧呈粥 阅读(98) 评论(0) 推荐(0) 编辑
摘要:import collections as con dq = con.deque() # 右侧添加 dq.append(1) print(dq, 'append') # 左侧添加 dq.appendleft(2) print(dq, dq[0], 'appendleft') # 清空队列 # dq.clear() # 队列某元素数量 print(dq.count(1), 'count'... 阅读全文
posted @ 2017-03-12 18:12 破斧呈粥 阅读(112) 评论(0) 推荐(0) 编辑
摘要:import queue # 参数指定队列大小 q = queue.Queue(10) print(q) # 查看当前队列数量 print(q.qsize(), 'qsize') # 当前队列是否为空 print(q.empty()) #当前队列数量是否已达到队列最大值 print(q.full(),'full') #加入一个队列成员 q.put(10) q.put(1) print(... 阅读全文
posted @ 2017-03-12 18:11 破斧呈粥 阅读(116) 评论(0) 推荐(0) 编辑
摘要:import copy # 浅拷贝 # copy.copy() # 深拷贝 # copy.deepcopy() # 赋值 s1 = '1234' s2 = 456 t1 = s1 t2 = s2 c1 = copy.copy(s1) c2 = copy.deepcopy(s1) print(id(s1), id(t1), id(c1), id(c2), id(s2), id(t2)) #... 阅读全文
posted @ 2017-03-12 18:05 破斧呈粥 阅读(152) 评论(0) 推荐(0) 编辑
摘要:# 无参数 def fun1(): print(1, '无参数') fun1() # 带返回值 def fun2(): return 999 print(fun2(), '带返回值') # 有参数 def fun3(a, b): print(a, b, '带参数') fun3(1, 3) # 默认参数 def fun4(a, b=333): print... 阅读全文
posted @ 2017-03-12 18:03 破斧呈粥 阅读(567) 评论(0) 推荐(0) 编辑
摘要:''' abs() dict() help() min() setattr() all() dir() hex() next() slice() any() divmod() id() object() sorted() ascii() enumerate() input() oct() staticmethod() bin() eval() int() open() str() bool() ... 阅读全文
posted @ 2017-03-12 18:01 破斧呈粥 阅读(138) 评论(0) 推荐(0) 编辑
摘要:luck_num = 10 num = 0 while luck_num != num: num = int(input('please input you num:')) if num > luck_num: print('Greater than lucknum') elif num luck_num: print('Greater... 阅读全文
posted @ 2017-03-03 22:14 破斧呈粥 阅读(114) 评论(0) 推荐(0) 编辑
摘要:import os # 判断文件是否存在 if not os.path.exists('error.txt'): f = open('error.txt', 'w') listname = [] else: # 读取文件内容 f = open('error.txt', 'r') listname = f.readlines() f.close() #... 阅读全文
posted @ 2017-03-03 22:14 破斧呈粥 阅读(149) 评论(0) 推荐(0) 编辑
摘要:zunyi = ['红花岗', '汇川', '遵义'] liupanshui = ['钟山', '六枝特', '水城'] guiyang = ['南明', '云岩', '花溪'] guizhou = {'1:贵阳': guiyang, '2:六盘水': liupanshui, '3:遵义': zunyi} guilin = ['秀峰', '叠彩', '象山' 阅读全文
posted @ 2017-03-03 22:14 破斧呈粥 阅读(137) 评论(0) 推荐(0) 编辑
摘要:# 余额 Money = 10000 # 购物车列表 shopping_cart = [] # 商品列表,可以用list代替,用dict是为了练习 commodity = { '男装': 318, '女装': 218, '内衣': 118, '鞋': 98, '箱包': 546, '配件': 342, '童装玩具': 32, '... 阅读全文
posted @ 2017-03-03 22:14 破斧呈粥 阅读(185) 评论(0) 推荐(0) 编辑
摘要:# 列表操作 nlist = ['123', '10', '982', '123'] nlist.append('4') # 添加一个元素 a = nlist.index('10') # 查找元素索引,返回第一次出现的索引,后两个参数指定开始和结束位置,如果未找到会报错 # 查找所有元素位置1 # enumerate将序列按下标和元素顺序依次取出 print([i for i, x in ... 阅读全文
posted @ 2017-03-03 22:13 破斧呈粥 阅读(314) 评论(0) 推荐(0) 编辑
摘要:#'w':只写,不能读(文件不存在会创建,存在会先删除原始内容) #'r':只读,不能写 #'a':追加,可写,不能读(文件不存在会创建,存在则追加内容) #'r+':可读,可写,可追加 #'w+':写读,先写后读(文件存在会先删除原始内容) #'a+':同'a' #'rU':读取时,将\r \n \r\n 转换成\n,功能同'r' #'r+U':读取时,将\r \n \r\n 转换成\n,... 阅读全文
posted @ 2017-03-03 22:13 破斧呈粥 阅读(204) 评论(0) 推荐(0) 编辑
摘要:import sys print(sys.argv) 阅读全文
posted @ 2017-03-03 22:13 破斧呈粥 阅读(65) 评论(0) 推荐(0) 编辑
摘要:dic = {'a1': 4, 2: '2', '3': [1, 2, 3], 4: {1: 10, 2: 20}} #dic = dict(a1='4',a2='5') # 清空字典dic.clear() # 取值 print(dic['3'][1]) # 后面的键会覆盖之前的重复键 print({'a': 12, 'b': 14, 'a': 18}) # 赋值,如果键不存在,就新增这... 阅读全文
posted @ 2017-03-03 22:13 破斧呈粥 阅读(185) 评论(0) 推荐(0) 编辑
摘要:1 #不推荐这种方式,因为每个要拼接的字符串都在不同内存位置 2 a = 'Computer' 3 b = 11 4 c = 16.5 5 print('name:' + a + 'age:' + str(b) + 'job:' + str(c),'方式一') 6 7 # 字符拼接(%s字符串变变量,%d=整数变量,%f=浮点数变量) 8 print(''' 9 ... 阅读全文
posted @ 2017-03-03 22:12 破斧呈粥 阅读(100) 评论(0) 推荐(0) 编辑
摘要:1 # r开头的函数是倒序 2 s = 'Helllo, WOr\tlld' 3 4 # 是否包含 5 print(s.__contains__('he'), 'he' in s) 6 7 # 首字母大写 8 print(s.capitalize(), 'capitalize') 9 10 # 大写变小写 11 print(s.casefold... 阅读全文
posted @ 2017-03-03 22:12 破斧呈粥 阅读(257) 评论(0) 推荐(0) 编辑
摘要:1 f = 1.0 2 # 转换成分数形式,如1.5=(3,2)=2分之3 3 print(f.as_integer_ratio()) 4 5 # 十六进制转浮点数 6 print(f.fromhex('eff')) 7 8 # 转成十六进制 9 print(f.hex()) 10 11 #是否为整 阅读全文
posted @ 2017-03-03 22:12 破斧呈粥 阅读(829) 评论(0) 推荐(0) 编辑
摘要:1 a = 1886 2 # 取商和余数 3 print(a.__divmod__(10)) 4 5 # r=反转,想当于 10-1886 6 print(a.__rsub__(10)) 7 8 # 取绝对值 9 print(a.__abs__(), abs(a)) 10 11 #商取整 12 pr 阅读全文
posted @ 2017-03-03 22:12 破斧呈粥 阅读(194) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示