python 3.0内置函数map、filter

官网链接

filter(function, iterable)¶

Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.

See itertools.filterfalse() for the complementary function that returns elements of iterable for which function returns false.nie 
filter说明
map(function, iterable, ...)
Return an iterator that applies function to every item of iterable, yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see itertools.starmap().
map说明
  • map

说明:遍历序列,对序列中每个元素进行操作,最终获取新的序列。

例子

>>> li = [11, 22, 33]
>>> new_list = map(lambda a: a + 100, li)
>>> for n in new_list: print(n)

111
122
133

>>> new_list = map(lambda a: a + 100, li)
>>> zyl = list(new_list)
>>> zyl
[111, 122, 133]

 

>>> zyl = map(str, range(5))
>>> for i in zyl: print(i)

0
1
2
3
4
>>> zyl01 = map(add, range(5))
>>> for i in zyl01: print(i)

0
2
4
6
8
>>> zyl02 = map(lambda x: x+1, range(5))
>>> tj = list(zyl02)
>>> tj
[1, 2, 3, 4, 5]
>>> zyl03 = map(add, 'zhayilig')
>>> tj01 = list(zyl03)
>>> tj01
['zz', 'hh', 'aa', 'yy', 'ii', 'll', 'ii', 'gg']

 

>>> def add(x,y):return x+y

>>> zyl04 = map(add, 'zhagyiligon','lovepython')
>>> tj02 = list(zyl04)
>>> tj02
['zl', 'ho', 'av', 'ge', 'yp', 'iy', 'lt', 'ih', 'go', 'on']
>>> zyl05= map(add, 'zhagyilig','python')
>>> tj03 = list(zyl05)
>>> tj03
['zp', 'hy', 'at', 'gh', 'yo', 'in']

  • filter

说明:对于序列中的元素进行筛选,最终获取符合条件的序列; 对序列中的item依次执行 function(item),将执行结果为True(!=0)的item组成一个List/String/Tuple(取决于sequence的类型)返回,False则退出(0),进行过滤;过滤返回值为1。

例子

>>> li = [11, 22, 33]
>>> new_list = filter(lambda a: a > 20, li)
>>> zyl = list(new_list)
>>> zyl
[22, 33]

>>> fil = filter(div, range(6))
>>> zyl = list(fil)
>>> zyl
[1, 3, 5]

>>> fil = filter(lambda x : x%2,range(10))   #lambda 函数返回奇数,返回列表
>>> zyl = list(fil)
>>> zyl
[1, 3, 5, 7, 9]

>>> fil = filter(lambda x : not x%2,range(10))
>>> zyl = list(fil)
>>> zyl
[0, 2, 4, 6, 8]

 

>>> def fin(n):return n!='z'  #过滤'z' 函数,出现z则返回False

>>> fil = filter(fin, 'zhgyilig') 
>>> for i in fil:print(i)

h
g
y
i
l
i
g

>>> fil = filter(lambda x: x != 'z', 'zhgyilig')  #labmda返回True值
>>> zyl = list(fil)
>>> zyl
['h', 'g', 'y', 'i', 'l', 'i', 'g']
>>> fil = filter(lambda x: x == 'z', 'zhgyilig')  #返回:字符串
>>> zyl = list(fil)
>>> zyl
['z']

 实例:将100~200以内的质数挑选出来

思路:

质数是指:只有1和它本身两个因数,如2、3、5、7都是质数,即能被1和本身整除,1不是质数。
比如:数字N,判断是否质数,就须判断:能能不能整除【2,N】之间的数X(不包含本身),即N%X是否为0,要是没有就为质数。

#!/usr/bin/env python
# Author:zhagyilig


def is_prime(start,stop):
    stop = stop + 1
    prime = filter(lambda x: not [x%i for i in range(2,x) if x%i == 0],range(start,stop))
    print(list(prime))


if __name__ == '__main__':
    try:
        start = int(input("enter a start number: "))
    except:
        start = int(2)
    try:
        stop = int(input("enter a stop number: "))
    except:
        start = int(0)

    is_prime(start,stop)
View Code

转载于:https://www.cnblogs.com/zhagyilig/p/6970959.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值