python技巧(一)
来源:互联网 发布:男主是人工智能的小说 编辑:程序博客网 时间:2024/05/22 11:39
1.参数unpack
*args表示任何多个无名参数,它是一个tuple;**kwargs表示关键字参数,它是一个 dict。并且同时使用*args和**kwargs时,必须*args参数列要在**kwargs前。
如此可以简单的给函数传递列表和字典
def draw_point(x, y): # do some magicpoint_foo = (3, 4)point_bar = {'y': 3, 'x': 2}draw_point(*point_foo)draw_point(**point_bar)
2.枚举器
>>> a = ['a', 'b', 'c', 'd', 'e']>>> for index, item in enumerate(a): print index, item...0 a1 b2 c3 d4 e>>>
3.for/else
语法糖,但能是逻辑更清晰for i in foo: if i == 0: breakelse: print("i was never 0")相比原始写法
found = Falsefor i in foo: if i == 0: found = True breakif not found: print("i was never 0")
4.新型格式化
可读性更好,不容易对错位置found = False>>> print "The %(foo)s is %(bar)i." % {'foo': 'answer', 'bar':42}The answer is 42.>>> foo, bar = 'question', 123>>> print "The %(foo)s is %(bar)i." % locals()The question is 123.
5.使用site-package中的.pth
增加或使用第三方哭,或对其版本配置时,使用PATHPATH环境变量不太方便可以修改site-package中的.pth
"The most convenient way [to modify python's search path] is to add a path configuration file to a directory that's already on Python's path, usually to the .../site-packages/ directory. Path configuration files have an extension of .pth, and each line must contain a single path that will be appended to sys.path. (Because the new paths are appended to sys.path, modules in the added directories will not override standard modules. This means you can't use this mechanism for installing fixed versions of standard modules.)"
6.dict的get函数
7.两个值交换
>>> a = 10>>> b = 5>>> a, b(10, 5)>>> a, b = b, a>>> a, b(5, 10)
8.doctest
单元测试写到注释中def factorial(n): """Return the factorial of n, an exact integer >= 0. If the result is small enough to fit in an int, return an int. Else return a long. >>> [factorial(n) for n in range(6)] [1, 1, 2, 6, 24, 120] >>> factorial(-1) Traceback (most recent call last): ... ValueError: n must be >= 0 Factorials of floats are OK, but the float must be an exact integer: """ import math if not n >= 0: raise ValueError("n must be >= 0") if math.floor(n) != n: raise ValueError("n must be exact integer") if n+1 == n: # catch a value like 1e300 raise OverflowError("n too large") result = 1 factor = 2 while factor <= n: result *= factor factor += 1 return resultdef _test(): import doctest doctest.testmod() if __name__ == "__main__": _test()
9.list切块
a = [1,2,3,4,5]>>> a[::2] # iterate over the whole list in 2-increments[1,3,5]
>>> a[::-1][5,4,3,2,1]
10.三元运算
x = 3 if (y == 1) else 2 if (y == -1) else 1
- python技巧(一)
- Python编程技巧(一)
- python技巧(一)
- Python 技巧(一)
- python技巧一
- python使用技巧(一)
- Python高级技巧(一)
- Python进阶(一):python技巧
- Python写爬虫技巧(一):总
- python技巧之下划线(一)
- Python数据分析(一): ipython 技巧!
- Python代码优化及技巧笔记(一)
- Python常用函数与技巧总结(一)
- python技巧
- python技巧
- python技巧
- python技巧
- Python 技巧
- jqGrid与Struts2的结合应用(八) —— 奇妙的subGrid子表格
- 用C语言绘制直方图,绘制输入单词长度的直方图
- 使用map标签选择图片区域
- Android SQLite
- jqGrid与Struts2的结合应用(九) —— 补充细节
- python技巧(一)
- 超分辨率工具箱
- 日期控件--My97DatePicker
- jqGrid与Struts2的结合应用(十) —— 真正的自定义表单查询
- Oauth1
- udp 发送一个整数 udpclient
- 谈Map/Reduce对分区与分组的理解
- Coding一下“反转单链表”,当是下周面试的练笔
- WebView download 文件