您的位置:首页 > 编程语言 > Python开发

python 模块学习备忘

2014-10-08 14:10 232 查看
下载huhamhire-hosts

要用到curse 模块

http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses

下载curses-2.2.win32-py2.7.exe

python的标准库学习:
https://python-documentation-cn.readthedocs.org/en/latest/library/index.html
build-in modules(write in C)

python列表排序:
http://gaopenghigh.iteye.com/blog/1483864
List的sort方法(或sorted内建函数)的用法
https://wiki.python.org/moin/HowTo/Sorting/
sorted(data, cmp=None, key=None, reverse=False)

1. 对由tuple组成的List排序

Python代码


>>> students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10),]

用key函数排序(lambda)

>>> sorted(students, key=lambda student : student[2]) # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

用cmp函数排序

Python代码


>>> sorted(students, cmp=lambda x,y : cmp(x[2], y[2])) # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

用 operator 函数来加快速度, 上面排序等价于:(itemgetter)

Python代码


>>> from operator import itemgetter, attrgetter
>>> sorted(students, key=itemgetter(2))

用 operator 函数进行多级排序

Python代码


>>> sorted(students, key=itemgetter(1,2)) # sort by grade then by age
[('john', 'A', 15), ('dave', 'B', 10), ('jane', 'B', 12)]

2. 对由字典排序

Python代码

>>> d = {'data1':3, 'data2':1, 'data3':2, 'data4':4}
>>> sorted(d.iteritems(), key=itemgetter(1), reverse=True)
[('data4', 4), ('data1', 3), ('data3', 2), ('data2', 1)]

yield
http://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/
使用的产生器,要使用next函数获取每次产生的返回值。最后会引起一个StopIteration的错误。一般使用for 循环获取每个值。

zip() in conjunction with the
* operator can be used to unzip alist:

>>>
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> zipped
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zipped)
>>> x == list(x2) and y == list(y2)
True


operator 模块为python提供了一个“功能性"的标准操作符接口,当使用map 或filter sorted 一类函数时,operator模块中的函数可以替换一些lambda函数。

types 模块包含了标准解析器定义的所有类型的类型对象。

=============================================================================================================

Python for Data Analysis
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: