# 补充几个高级函数
# zip
- 把两个可迭代内容生成一个可迭代的tuple元素类型组成的内容
# zip 案例 l1 = [ 1,2,3,4,5] l2 = [11,22,33,44,55] z = zip(l1, l2) print(type(z)) print(z) for i in z: print(i)l1 = ["wangwang", "mingyue", "yyt"] l2 = [89, 23, 78] z = zip(l1, l2) for i in z: print(i) # 考虑下面结果,为什么会为空 l3 = [i for i in z] print(l3)
# enumerate
- 跟zip功能比较像
- 对可迭代对象里的每一元素,配上一个索引,然后索引和内容构成tuple类型
# enumerate案例1 l1 = [11,22,33,44,55] em = enumerate(l1) l2 = [i for i in em] print(l2)em = enumerate(l1, start=100) l2 = [ i for i in em] print(l2)
# collections模块
- namedtuple
- deque
### namedtuple
- tuple类型
- 是一个可命名的tuple
import collections Point = collections.namedtuple("Point", ['x', 'y']) p = Point(11, 22) print(p.x) print(p[0])Circle = collections.namedtuple("Circle", ['x', 'y', 'r']) c = Circle(100, 150, 50) print(c) print(type(c)) # 想检测以下namedtuple到底属于谁的子类 isinstance(c, tuple)
# dequeue
- 比较方便的解决了频繁删除插入带来的效率问题
from collections import deque q = deque(['a', 'b', 'c']) print(q) q.append("d") print(q) q.appendleft('x') print(q)
# defaultdict
- 当直接读取dict不存在的属性时,直接返回默认值
d1 = {"one":1, "two":2, "three":3} print(d1['one']) print(d1['four'])
from collections import defaultdict # lambda表达式,直接返回字符串 func = lambda: "刘大拿" d2 = defaultdict(func) d2["one"] = 1 d2["two"] = 2 print(d2['one']) print(d2['four'])
# Counter
- 统计字符串个数
from collections import Counter # 为什么下面结果不把abcdefgabced.....作为键值,而是以其中每一个字母作为键值 # 需要括号里内容为可迭代 c = Counter("abcdefgabcdeabcdabcaba") print(c)s = ["liudana", "love", "love", "love", "love", "wangxiaona"] c = Counter(s) print(c)
# 调试技术 - 调试流程:单元测试->集成测试->交测试部 - 分类: - 静态调试: - 动态调试: # pdb调试 - 推荐文章 - [官方网页(英文)](https://docs.python.org/2/library/pdb.html) - [pdb模块介绍](http://blog.csdn.net/carolzhang8406/article/details/6923997) - [pdb调试技巧](https://www.ibm.com/developerworks/cn/linux/l-cn-pythondebugger/) - 简单使用的介绍,推荐优先阅读 - [pdb详细中文介绍](http://blog.csdn.net/wyb_009/article/details/8896744) - 主要是帮助文件的中文翻译 - [调试案例01](https://www.cnblogs.com/dkblog/archive/2010/12/07/1980682.html) - [调试案例02](http://python.jobbole.com/81184/) - pdb: python 调试库 # pycharm调试 - run/debug模式 - 案例01.py - 断点:程序的某一行,程序在debug模式下,遇到断点就会暂停 # 单元测试 - 推荐文档 - [官方测试文档集合](https://wiki.python.org/moin/PythonTestingToolsTaxonomy) - [测试案例01](http://blog.csdn.net/a542551042/article/details/46696635) - [PyUnit](https://wiki.python.org/moin/PyUnit) - [PyUnit详细讲解案例02](http://www.jb51.net/article/64119.htm) - [测试案例03](https://www.cnblogs.com/iamjqy/p/7155315.html)
def SayHello(name): print("I want to say hello with your, {0}".format(name)) print("Hello, {0}".format(name)) print("Done...............") if __name__ == "__main__": print('***' * 10) name = input("Please input your name: ") print(SayHello(name=name) ) print('@@@' * 10) # 测试案例