python的一些小函数很能提高效率,平时在工作中经常忽视这些内容,而使用很原始粗暴的方法写代码;写了一段时间以后,发现自己的提高很少,要写个小脚本也要纠结半天,跟那些大拿们相差太大;所以要检讨自己,看看自己可以从那方面提高自己的技术能力;
今天首先学习下python的实用小函数:
lamda() 返回一个函数表达式,类似于def,但是比def更轻巧,可以没有名字
add_by_lambda = lambda x,y: x+y print add_by_lambda(1, 1) 甚至还可以直接在后面追加实参来直接获取返回值,比如lambda x,y : x+y, 1, 1返回结果就是2 ------------------------------------- def add(x,y): return x+y print add(1, 1)
zip()
定义:zip([seql, ...])接受一系列可迭代对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的list(列表)。若传入参数的长度不等,则返回list的长度和参数中长度最短的对象相同。
1 >>> z1=[1,2,3] 2 >>> z2=[4,5,6] 3 >>> result=zip(z1,z2)
zip()配合*号操作符,可以将已经zip过的列表对象解压,即将合并的序列拆成多个tuple.
1 >>> zip(*result) 2 [(1, 2, 3), (4, 5, 6)]
与序列有关的内建函数有:sorted()、reversed()、enumerate()、zip()
sorted()和zip()返回一个序列(列表)对象
reversed()、enumerate()返回一个迭代器(类似序列)
http://www.cnblogs.com/BeginMan/archive/2013/03/14/2959447.html
场景:
* 二维矩阵变换(矩阵的行列互换)
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] zip(*a) [(1, 4, 7), (2, 5, 8), (3, 6, 9)] map(list,zip(*a)) [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
* 顺序获取数据
>>> name=('jack','beginman','sony','pcky') >>> age=(2001,2003,2005,2000) >>> for a,n in zip(name,age): print a,n 输出: jack 2001 beginman 2003 sony 2005 pcky 2000
zip高级应用:
1.zip打包解包列表和倍数 >>> a = [1, 2, 3] >>> b = ['a', 'b', 'c'] >>> z = zip(a, b) >>> z [(1, 'a'), (2, 'b'), (3, 'c')] >>> zip(*z) [(1, 2, 3), ('a', 'b', 'c')] 2. 使用zip合并相邻的列表项 >>> a = [1, 2, 3, 4, 5, 6] >>> zip(*([iter(a)] * 2)) [(1, 2), (3, 4), (5, 6)] >>> group_adjacent = lambda a, k: zip(*([iter(a)] * k)) >>> group_adjacent(a, 3) [(1, 2, 3), (4, 5, 6)] >>> group_adjacent(a, 2) [(1, 2), (3, 4), (5, 6)] >>> group_adjacent(a, 1) [(1,), (2,), (3,), (4,), (5,), (6,)] >>> zip(a[::2], a[1::2]) [(1, 2), (3, 4), (5, 6)] >>> zip(a[::3], a[1::3], a[2::3]) [(1, 2, 3), (4, 5, 6)] >>> group_adjacent = lambda a, k: zip(*(a[i::k] for i in range(k))) >>> group_adjacent(a, 3) [(1, 2, 3), (4, 5, 6)] >>> group_adjacent(a, 2) [(1, 2), (3, 4), (5, 6)] >>> group_adjacent(a, 1) [(1,), (2,), (3,), (4,), (5,), (6,)] 3.使用zip和iterators生成滑动窗口 (n -grams) >>> from itertools import islice >>> def n_grams(a, n): ... z = (islice(a, i, None) for i in range(n)) ... return zip(*z) ... >>> a = [1, 2, 3, 4, 5, 6] >>> n_grams(a, 3) [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)] >>> n_grams(a, 2) [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)] >>> n_grams(a, 4) [(1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6)] 4.使用zip反转字典 >>> m = {'a': 1, 'b': 2, 'c': 3, 'd': 4} >>> m.items() [('a', 1), ('c', 3), ('b', 2), ('d', 4)] >>> zip(m.values(), m.keys()) [(1, 'a'), (3, 'c'), (2, 'b'), (4, 'd')] >>> mi = dict(zip(m.values(), m.keys())) >>> mi {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
filter()
filter函数接受两个参数,func和list,而经过过滤后返回一个list,其中func函数对象只能有一个传入参数。原理便是根据列表list中所有元素作为参数传递给函数func,返回可以令func返回真的元素的列表,如果func为None,那么会使用默认的Python内置的identity函数直接判断元素的True or False。
例如:
a = [1,2,3,4,5,6,7] b=filter(lambda x:x>2, a) print b #过滤奇数集 a = [1,2,3,4,5,6,7] b=filter(lambda x:x%2, a) print b
map()
map函数是一个很强大的一个映射函数,其传入两个参数,一个是func,一个是list,而功效便是func作用于给定序列的每个元素,并用一个列表来提供返回值。例如:
a=[0,1,2,3,4,5,6,7] map(lambda x:x+3, a) a=[1,2,3] b=[4,5,6] map(lambda x,y:x+y, a,b) [5,7,9] #my_map函数实现 def my_map(func, *args): return [ func(arg) for arg in args ]
reduce()
reduce函数传入参数为func和list,其遍历list元素,并调用func函数实现累积,具体效果便是:
reduce(f, [x1, x2, x3, x4]) = f ( f ( f ( x1, x2 ), x3 ), x4 )
使用范例如下:
#str to int
def str2int(s):
return reduce(lambda x,y: x*10+y, map(int, s))
妙用集锦: http://devopstarter.info/pythonkai-fa-zhi-mapreduce/
#两个list,取(x - y) + (y - x)
x=[{'a': 1, 'b': 2}, {'c': 3}, {'d': 4}]
y=[{'a': 1}, {'c': 3}, {'e': 5}]
filter(lambda z: (x+y).count(z)<2, (x+y))
#flatten out nested sublist
#result: [ 1, 2, 3, 4, 5 ]
import operator
reduce( operator.concat, [ [ 1, 2 ], [ 3, 4 ], [ ], [ 5 ] ], [ ] )
#多项式求和
import operator
def evaluate (a, x):
xi = map( lambda i: x**i, range( 0, len(a)))
axi = map(operator.mul, a, xi)
return reduce( operator.add, axi, 0 )
#数据库SQL
reduce( max, map( Camera.pixels, filter(
lambda c: c.brand() == "Nikon", cameras ) ) )
#maybe equals
SELECT max(pixels)
FROM cameras
WHERE brand = “Nikon”
#There.
#cameras is a sequence
#where clause is a filter
#pixels is a map
#max is a reduce
#一行并发
import urllib2
from multiprocessing.dummy import Pool as ThreadPool
urls = [
'http://www.python.org',
'http://www.google.com',
'http://www.baidu.com',
'http://www.python.org/community/',
'http://www.saltstack.com'
]
#pool = ThreadPool()
pool = ThreadPool(4) # Sets the pool size to 4
result = pool.map(urllib2.urlopen, urls)
pool.close()
pool.join()
int() 转数字 int('0')
str() 转字符串str(2)
lower() 转小写lower(Windows)
upper() 转大写upper(Linux)
iter()
list.count('aaa') 统计aaa在列表中出现的次数