内置函数———filter

  1. def is_not_empty(s):
  2. return s and len(s.strip()) > 0
  3. filter(is_not_empty, ['test', None, '', 'str', ' ', 'END'])
  4.  
  5. 执行结果:
  6.  
  7. ['test', 'str', 'END']
 
 
  1. 注意:
  2. s.strip(rm) 删除 s 字符串中开头、结尾处的 rm 序列的字符。
  3.  
  4. rm为空时,默认删除空白符(包括'\n', '\r', '\t', ' '),如下:
  5.  
  6. >>> a = ' 123'
  7. >>> a.strip()
  8. '123'
  9.  
  10. >>> a = '\t\t123\r\n'
  11. >>> a.strip()
  12. '123'
 

map

map()函数应用于每一个可迭代的项,返回的是一个结果list。

如果有其他的可迭代参数传进来,map()函数则会把每一个参数都以相应的处理函数进行迭代处理。

map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回。

  1. # 有一个list, L = [1,2,3,4,5,6,7,8],我们要将f(x)=x^2作用于这个list上,那么我们可以# 使用map函数处理。
  2.  
  3. >>> L = [1,2,3,4,]
  4. >>> def pow2(x):
  5. ... return x*x
  6. ...
  7. >>> map(pow2,L)
  8. [1, 4, 9, 16]

  

内置函数——max

1. 函数功能为取传入的多个参数中的最大值,或者传入的可迭代对象元素中的最大值。默认数值型参数,取值大者;字符型参数,取字母表排序靠后者。还可以传入命名参数key,其为一个函数,用来指定取最大值的方法。default命名参数用来指定最大值不存在时返回的默认值。

2. 函数至少传入两个参数,但是有只传入一个参数的例外,此时参数必须为可迭代对象,返回的是可迭代对象中的最大元素。

 
  1. >>> max(1) # 传入1个参数报错
  2. Traceback (most recent call last):
  3. File "<pyshell#0>", line 1, in <module>
  4. max(1)
  5. TypeError: 'int' object is not iterable
  6. >>> max(1,2) # 传入2个参数 取2个中较大者
  7. 2
  8. >>> max(1,2,3) # 传入3个参数 取3个中较大者
  9. 3
  10. >>> max('1234') # 传入1个可迭代对象,取其最大元素值
  11. '4'
 

3. 当传入参数为数据类型不一致时,传入的所有参数将进行隐式数据类型转换后再比较,如果不能进行隐式数据类型转换,则会报错。

 
  1. >>> max(1,1.1,1.3E1) # 整数与浮点数可取最大值
  2. 13.0
  3. >>> max(1,2,3,'3') # 数值与字符串不能取最大值
  4. Traceback (most recent call last):
  5. File "<pyshell#5>", line 1, in <module>
  6. max(1,2,3,'3')
  7. TypeError: unorderable types: str() > int()
  8.  
  9. >>> max([1,2],[1,3]) # 列表与列表可取最大值
  10. [1, 3]
  11. >>> max([1,2],(1,3)) # 列表与元组不能取最大值
  12. Traceback (most recent call last):
  13. File "<pyshell#7>", line 1, in <module>
  14. max([1,2],(1,3))
  15. TypeError: unorderable types: tuple() > list()
 

. 当存在多个相同的最大值时,返回的是最先出现的那个最大值。

 
  1. #定义a、b、c 3个列表
  2. >>> a = [1,2]
  3. >>> b = [1,1]
  4. >>> c = [1,2]
  5.  
  6. #查看a、b、c 的id
  7. >>> id(a)
  8. 68128320
  9. >>> id(b)
  10. 68128680
  11. >>> id(c)
  12. 68128240
  13.  
  14. #取最大值
  15. >>> d = max(a,b,c)
  16. >>> id(d)
  17. 68128320
  18.  
  19. #验证是否最大值是否是a
  20. >>> id(a) == id(d)
  21. True
 
 

默认数值型参数,取值大者;字符型参数,取字母表排序靠后者;序列型参数,则依次按索引位置的值进行比较取最大者。还可以通过传入命名参数key,指定取最大值方法。

 
 
  1. >>> max(1,2) # 取数值大者
  2. 2
  3. >>> max('a','b') # 取排序靠后者
  4. 'b'
  5. >>> max('ab','ac','ad') # 依次按索引比较取较大者
  6. 'ad'
  7.  
  8. >>> max(-1,0) # 数值默认去数值较大者
  9. 0
  10. >>> max(-1,0,key = abs) # 传入了求绝对值函数,则参数都会进行求绝对值后再取较大者
  11. -1
 
 

key参数的另外一个作用是,不同类型对象本来不能比较取最大值的,传入适当的key函数,变得可以比较能取最大值了。

 
 
  1. >>> max(1,2,'3') #数值和字符串不能取最大值
  2. Traceback (most recent call last):
  3. File "<pyshell#21>", line 1, in <module>
  4. max(1,2,'3')
  5. TypeError: unorderable types: str() > int()
  6. >>> max(1,2,'3',key = int) # 指定key为转换函数后,可以取最大值
  7. '3'
  8.  
  9. >>> max((1,2),[1,1]) #元组和列表不能取最大值
  10. Traceback (most recent call last):
  11. File "<pyshell#24>", line 1, in <module>
  12. max((1,2),[1,1])
  13. TypeError: unorderable types: list() > tuple()
  14. >>> max((1,2),[1,1],key = lambda x : x[1]) #指定key为返回序列索引1位置的元素后,可以取最大值
  15. (1, 2)
 

当只传入的一个可迭代对象时,而且可迭代对象为空,则必须指定命名参数default,用来指定最大值不存在时,函数返回的默认值。

  1. >>> max(()) #空可迭代对象不能取最大值
  2. Traceback (most recent call last):
  3. File "<pyshell#26>", line 1, in <module>
  4. max(())
  5. ValueError: max() arg is an empty sequence
  6. >>> max((),default=0) #空可迭代对象,指定default参数为默认值
  7. 0
  8. >>> max((),0) #默认值必须使用命名参数进行传参,否则将被认为是一个比较的元素
  9. Traceback (most recent call last):
  10. File "<pyshell#27>", line 1, in <module>
  11. max((),0)
  12. TypeError: unorderable types: int() > tuple()

  

内置函数:min 用法

源码

  1. def min(*args, key=None): # known special case of min
    """
    min(iterable, *[, default=obj, key=func]) -> value
    min(arg1, arg2, *args, *[, key=func]) -> value
  2.  
  3. With a single iterable argument, return its smallest item. The
    default keyword-only argument specifies an object to return if
    the provided iterable is empty.
    With two or more arguments, return the smallest argument.
    """
    pass

基础用法

  1. tes = min(1,2,4)
  2. print(tes)
  3. #可迭代对象
  4. a = [1, 2, 3, 4, 5, 6]
  5. tes = min(a)
  6. print(tes)

key属性的使用

当key参数不为空时,就以key的函数对象为判断的标准。
如果我们想找出一组数中绝对值最小的数,就可以配合lamda先进行处理,再找出最小值

  1. a = [-9, -8, 11, 23, -4, 6]
  2. tes = min(a, key=lambda x: abs(x))
  3. print(tes)

高级技巧:找出字典中值最小的那组数据

如果有一组商品,其名称和价格都存在一个字典中,可以用下面的方法快速找到价格最贵的那组商品:

 
  1. prices = {
  2. 'A':123,
  3. 'B':450.1,
  4. 'C':12,
  5. 'E':444,
  6. }
  7. # 在对字典进行数据操作的时候,默认只会处理key,而不是value
  8. # 先使用zip把字典的keys和values翻转过来,再用min取出值最小的那组数据
  9. min_prices = min(zip(prices.values(), prices.keys()))
  10. print(min_prices) # (450.1, 'B')
 

当字典中的value相同的时候,才会比较key:

 
  1. prices = {
  2. 'A': 123,
  3. 'B': 123,
  4. }
  5.  
  6. min_prices = min(zip(prices.values(), prices.keys()))
  7. print(min_prices) # (123, 'B')
  8.  
  9. min_prices = min(zip(prices.values(), prices.keys()))
  10. print(min_prices) # (123, 'A')
 

和max用法基本一致

带有key参数的函数filter,map,max,min的更多相关文章

  1. python四个带 key 参数的函数(max、min、map、filter)

    四个带 key 参数的函数: max()点击查看详细 min()点击查看详细 map()点击查看详细 filter()点击查看详细 1)max(iterable, key) key:相当于对可迭代对象 ...

  2. 高阶函数 filter map reduce

    const app=new Vue({ el:'#app', data:{ books:[{ id:1, name:"算法导论", data: '2006-1', price:39 ...

  3. python 装饰器 第六步:带有收集参数的函数的装饰器

    #第六步:带有收集参数的函数的装饰器 #装饰器函数 def kuozhan(func): #内部函数(扩展之后的eat函数) def neweat(*w,**n): #以下三步就是扩展之后的功能,于是 ...

  4. Python内置函数filter, map, reduce

    filter.map.reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车. 1. filter函数的功能相当 ...

  5. Python 内置函数&filter()&map()&reduce()&sorted()

    常用内置函数 Python 2.x 返回列表,Python 3.x 返回迭代器 在进行筛选或映射时,输出的结果是一个数组,需要list帮助. 如:print(list(map(lambda x:x+1 ...

  6. python关于list的三个内置函数filter(), map(), reduce()

    ''' Python --version :Python 2.7.11 Quote : https://docs.python.org/2/tutorial/datastructures.html#m ...

  7. C++ (带有默认参数的函数参数)缺省函数参数

    缺省参数?在C++中,允许实参的个数与形参的个数不同.在声明函数原型时,为一个或者多个形参指定默认值,以后调用这个函数时,若省略某一个实参,c++则自动的以默认值作为相应参数的值. 实列说明:#inc ...

  8. 常用函数-filter、map、reduce、sorted

    常用函数 filter map reduce sorted和列表自带sort 待续... 一.filter函数 1.说明 filter()函数接收一个函数 f 和一个可迭代对象,这个函数 f 的作用是 ...

  9. js高阶函数filter、map、reduce

    // 高阶函数 filter/map/reduce // filter中的回调函数有一个要求:必须返回一个boolean值, // 当返回true时,函数内部会自动将这次回调的 n 加入到新的数组中 ...

随机推荐

  1. 调用sort段错误问题

    问题:sort的比较函数实现有问题导致进程调用sort时core了. 结论:特别要注意,sort的比较函数必须遵循严格弱排序(strict weak ordering)的规则.   这是最近在工作中遇 ...

  2. Codeforces Round #453 ( Div. 2) Editorial ABCD

    A. Visiting a Friend time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. code M资格赛 补题

    A: 音乐研究 时间限制:1秒 空间限制:32768K 美团外卖的品牌代言人袋鼠先生最近正在进行音乐研究.他有两段音频,每段音频是一个表示音高的序列.现在袋鼠先生想要在第二段音频中找出与第一段音频最相 ...

  4. ubuntu16安装navicat字体显示不正常,显示方框以及字体倒立

    昨天遇到了这个问题,网上找了很多方法都没有真正解决这个问题. 目前其他博客论坛说的主要方法有 1)将安装目录下的./start_navicat中的字符集改为zh_CN.UTF-8 2)将系统的默认字符 ...

  5. js 封装的自动创建表格的相关操作

    因为要做一个动态输入的表格,现在积累一下资料,在网上找了一些资料,经过总结是使用更加方便些,谁有更好的插件和封装的东西,请大家分享一下. <script type="text/java ...

  6. 在Linux系统环境下修改MySQL的root密码

    root用户登录系统 /usr/local/mysql/bin/mysqladmin -u root -p password 新密码 enter password 旧密码 第二种方法: root用户登 ...

  7. HDU 4638 Group (2013多校4 1007 离线处理+树状数组)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  8. Fork & vfork & clone (转载)

    转自:http://blog.csdn.net/zqy2000zqy/archive/2006/09/04/1176924.aspx 进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合, ...

  9. Ping Pod不通问题定位及Ingress验证

    Ping Pod网络问题不通定位记录 1.验证墙是否通 flannel默认使用8285端口作为UDP封装报文的端口,VxLan使用8472端口,下面命令验证一下确定其在8472端口 ip -d lin ...

  10. 万里长征第二步——django个人博客(第一步 ——创建主页)

    运行命令行工具,输入:pip install virtualenv  --安装virtualenv库. virtualenv blog_project_venv ——使用virtualenv创建一个虚 ...