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:
>>>
operator 模块为python提供了一个“功能性"的标准操作符接口,当使用map 或filter sorted 一类函数时,operator模块中的函数可以替换一些lambda函数。
types 模块包含了标准解析器定义的所有类型的类型对象。
=============================================================================================================
Python for Data Analysis
要用到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
相关文章推荐
- Python Import机制备忘-模块搜索路径(sys.path)、嵌套Import、package Import
- Python相关模块学习1 处理文件名模块glob
- Python学习——struct模块的pack、unpack示例
- Python模块学习——optparse
- Python模块学习——os
- Python 学习笔记 - 11.模块(Module)
- Python模块学习——subprocess
- Python学习——struct模块的pack、unpack示例
- Python学习——struct模块的pack、unpack示例
- Python模块学习——tempfile
- Python Import机制备忘-模块搜索路径(sys.path)、嵌套Import、package Import
- python模块学习 ---- smtplib 邮件发送
- python模块学习 ---- smtplib 邮件发送
- Python学习——struct模块的pack、unpack示例
- Python 学习手记 pt5 模块
- Python Import机制备忘-模块搜索路径(sys.path)、嵌套Import、package Import
- Python Import机制备忘-模块搜索路径(sys.path)、嵌套Import、package Import
- Python模块学习 ---- logging 日志记录(一)
- python中sys模块各个类变量和方法(译文)(随学习加深而更新)
- Python模块学习——email