python 模块学习备忘

下载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代码   收藏代码
  1. >>> students = [('john''A'15), ('jane''B'12), ('dave''B'10),] 
用key函数排序(lambda)
  1. >>> sorted(students, key=lambda student : student[2])   # sort by age  
  2. [('dave''B'10), ('jane''B'12), ('john''A'15)]
用cmp函数排序
Python代码   收藏代码
  1. >>> sorted(students, cmp=lambda x,y : cmp(x[2], y[2])) # sort by age  
  2. [('dave''B'10), ('jane''B'12), ('john''A'15)] 
用 operator 函数来加快速度, 上面排序等价于:(itemgetter)
Python代码   收藏代码
  1. >>> from operator import itemgetter, attrgetter  
  2. >>> sorted(students, key=itemgetter(2)) 
用 operator 函数进行多级排序
Python代码   收藏代码
  1. >>> sorted(students, key=itemgetter(1,2))  # sort by grade then by age  
  2. [('john''A'15), ('dave''B'10), ('jane''B'12)] 
2. 对由字典排序
Python代码  
  1. >>> d = {'data1':3'data2':1'data3':2'data4':4}  
  2. >>> sorted(d.iteritems(), key=itemgetter(1), reverse=True)  
  3. [('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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值