python质数列_Python中常用内置函数介绍(filter,map,reduce,apply,zip)

Python是一门很简洁,很优雅的语言,其很多内置函数结合起来使用,可以使用很少的代码来实现很多复杂的功能,如果同样的功能要让C/C++/Java来实现的话,可能会头大,其实Python是将复杂的数据结构隐藏在内置函数中,只要写出自己的业务逻辑Python会自动得出你想要的结果。这方面的内置函数主要有,filter,map,reduce,apply,结合匿名函数,列表解析一起使用,功能更加强大.使用内置函数最显而易见的好处是:

1. 速度快,使用内置函数,比普通的PYTHON实现,速度要快一倍左右。相当于C/C++的速度

2. 代码简洁

filter:

语法:

>>> help(filter)

Help on built-in function filter in module __builtin__:

filter(...)

filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true. If

function is None, return the items that are true. If sequence is a tuple

or string, return the same type, else return a list.

用途:

用于过滤与函数func()不匹配的值, 类似于SQL中select value != ‘a‘

相当于一个迭代器,调用一个布尔函数func来迭代seq中的每个元素,返回一个是bool_seq返回为True的序列

>>>第一个参数: function or None, 函数或None

>>>第二个参数: sequence,序列

说明:

>>>如果第一个参数为function,那么返回条件为真的序列(列表,元祖或字符串)

>>>如果第一个参数为None的话,那么返回序列中所有为True的项目

例1: 要过滤掉所有值为False的列表

print filter(None,[-2,0,2,'',{},()]) #输出[-2,2],其余的都为False

例2: 要过滤某个字母

>>> filter(lambda x: x !=‘a‘,‘abcd‘)

‘bcd‘

例3: 过滤字母以B开头的人名

>>> names = [‘Alice‘,‘Bob‘,‘Smith‘,‘David‘,‘Barbana‘]

>>> filter(lambda x: x.startswith(‘B‘),names)

[‘Bob‘, ‘Barbana‘]

例4: 要过滤fib列表中的奇数,偶数项

>>> fib = [0,1,1,2,3,5,8,13,21]

>>> filter(lambda x: x%2,fib)   #实际上等同于x%2 == 1的项才过滤

[1, 1, 3, 5, 13, 21]

>>> filter(lambda x: x%2 ==0,fib)

[0, 2, 8]

例5: 要将2-20间所有质数列出来

>>> filter(lambda x: not [x%i for i in range(2,x) if x%i == 0],range(2,20))

[2, 3, 5, 7, 11, 13, 17, 19]

例6: 要过滤所有子列表中,单词为‘Python‘的.

def filter_word(word):

try:

return word != 'Python'

except ValueError:

return False

words = [['Perl','Python','Shell'],['Java','C/C++'],['VB','Dephi']]

print [filter(filter_word,word) for word in words]

#用了列表解析的方法

filter的逻辑实现:

def filter(func,seq):

f_seq = [] #建一个空序列,用于存储过滤后的元素

for item in seq: #对序列中的每个元素进行迭代

if func(item): #如果为真的话

f_seq.append(item) #满足条件者,则加入

return f_seq #返回过滤后的元素

print filter(lambda x: x> 0,[-2,0, 2]) #对匿名函数进行过滤,返回正值

>>>

[2]map:>>> help(map)

Help on built-in function map in module __builtin__:

map(...)

map(function, sequence[, sequence, ...]) -> list

Return a list of the results of applying the function to the items of

the argument sequence(s). If more than one sequence is given, the

function is called with an argument list consisting of the corresponding

item of each sequence, substituting None for missing values when not all

sequences have the same length. If the function is None, return a list of

the items of the sequence (or a list of tuples if more than one sequence).用途:

>>>对一个及多个序列执行同一个操作,返回一个列表

说明:

1. 返回一个列表,该列表是参数func对seq1,seq2处理的结果集

2. 可以有多个序列,如果函数为None的话,返回一个序列的列表

例1:常规用法

>>> map(lambda x: x+1,[1,2,3,4])

[2, 3, 4, 5]

>>> map(lambda x,y: x+y,[1,2,3,4],(10,20,30,40))

[11, 22, 33, 44]

>>> map(lambda x,y: x+y if y else x+10,[1,2,3,4,5],(1,2,3,4))

#第一个序列中的第五个元素存在,但在第二个序列中不存在,所以y为False,所以执行5+10

[2, 4, 6, 8, 14]

>>> map(None,[1,2,3,4,5],(1,2)) #如果是None的话,以None来补齐短序列造成的空缺

[(1, 1), (2, 2), (3, None), (4, None), (5, None)]

>>> names = [‘Alice‘,‘Jerry‘,‘Bob‘,‘Barbar‘]

>>> map(len,names)        #求列表中每个元素的长度

[5, 5, 3, 6]

例2: 求0-5之间数,[本身,平方,立方],如:元素2,则返回:[2,4,8]

def func1(x): return x #返回自身

def func2(x): return x ** 2 #返回平方

def func3(x): return x ** 3 #返回立方

funcs = [func1,func2,func3] #函数列表

for i in range(5): #遍历列表

print map(lambda func: func(i),funcs)#对其中每个元素执行func1(i),func2(i),func3(i)操作例3: 实现下面的逻辑结构

1.0 [1,2,3,4,5]

2.0 [1,2,3,4,5]

....

foos = [1.0,2.0,3.0,4.0,5.0]

bars = [1,2,3,4,5]

def test(foo):

print foo,bars

print map(test,foos)map的实现逻辑def map(func,seq):

map_seq = [] #建空序列

for item in seq: #对序列中每个元素进行处理

map_seq.append(func(item)) #往空序列中添加func处理过的元素

return map_seq #返回最后的列表

print map(lambda x: x * 2,[1,2,3,4]) #[2,4,6,8]reduce:

用途:

func为二元函数,将func作用于seq序列的元素,每次携带一对(先前的结果以及下一个序列的元素),连续的将现有的结果和下一个值作用在获得的随后的结果上,最后减少我们的序列为一个单一的返回值:如果初始值init给定,第一个比较会是init和第一个序列元素而不是序列的头两个元素。

说明:

>>> help(reduce)

Help on built-in function reduce in module __builtin__:

reduce(...)

reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,

from left to right, so as to reduce the sequence to a single value.

For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates

((((1+2)+3)+4)+5). If initial is present, it is placed before the items

of the sequence in the calculation, and serves as a default when the

sequence is empty.

例子:

>>> reduce(lambda x,y: x+y, [47,11,42,13])

113

其实现过程如下图所示:

03a8ea91af4def51c51d94be30ef2cff.png

reduce的逻辑实现:

def reduce(func,seq,init=None):

l_seq = list(seq) #先转为列表

if init is None: #如果初始值

res = l_seq.pop(0)

else:

res = init

for item in l_seq:

res = func(res,item) #func(res,item)作为结果集传给res

return res #返回结果集

print reduce(lambda x,y:x+y,[1,2,3,4]) #结果为10

print reduce(lambda x,y:x+y,[1,2,3,4],10) #结果为20,init初始值为10apply:

语法:

>>> help(apply)

Help on built-in function apply in module __builtin__:

apply(...)

apply(object[, args[, kwargs]]) -> value

Call a callable object with positional arguments taken from the tuple args,

and keyword arguments taken from the optional dictionary kwargs.

Note that classes are callable, as are instances with a __call__() method

Deprecated since release 2.3. Instead, use the extended call syntax:

function(*args, **keywords).

用途:

>>>当一个函数的参数存在于一个元组或者一个字典中时,用来间接的调用这个函数,元组或者字典中的参数按照顺序传递

说明:

1. args是一个包含按照函数所需参数传递的位置参数的一个元组,假如func(a=1,b=2),那么这个元组中就必须严格按照这个参数的位置顺序进行传递(a=3,b=4),而不能是(b=4,a=3)这样的顺序

2. kwargs是一个包含关键字参数的字典,而其中args如果不传递,kwargs需要传递,则必须在args的位置留空

3. apply函数的返回值就是func函数的返回值.

例1: 常规使用

def func1(): #无参函数

print 'No Args!'

def func2(arg1,arg2): #两个参数

print arg1,arg2

def func3(arg1=1,arg2=2): #带字典函数

print arg1,arg2

if __name__=='__main__':

apply(func1)

apply(func2,('Hello','World!'))

apply(func3,(),{'arg1':'This is param1','arg2':'This is param2'}) #注意元祖参数为()

zip

>>> help(zip)

Help on built-in function zip in module __builtin__:

zip(...)

zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]

Return a list of tuples, where each tuple contains the i-th element

from each of the argument sequences. The returned list is truncated

in length to the length of the shortest argument sequence.用途:

>>>返回一个元祖列表,该元祖按顺序包含每个序列的相应元素,以最小的一个为准

说明:

>>>这个内置函数其实比较好理解,返回的对象就是一个元祖列表,看例子就好明白

例子:

>>> zip(range(5),range(1,20,2))

[(0, 1), (1, 3), (2, 5), (3, 7), (4, 9)]

>>> x=(1,2,3); y=(4,5);z=(6,)

>>> zip(x,y,z)

[(1, 4, 6)]

可以参看这个综合例子:

http://www.360doc.com/content/14/0507/11/7821691_375450523.shtml

http://my.oschina.net/cloudcoder/blog/226461

原文:http://blog.csdn.net/jerry_1126/article/details/41143579

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值