1. map函数 :处理序列中的每个元素,得到的结果是一个列表,该列表元素个数及位置与原来一样
  2.  
  3. ## 求列表里元素的平方 (原始方法)
  4. num_1=[1,2,13,5,8,9]
  5. res =[]
  6. for i in num_1:
  7. res.append(i**2)
  8. print('打印结果:',res)
  9.  
  10. 打印结果: [1, 4, 169, 25, 64, 81]
  11.  
  12. 有多个列表求里面元素的平方 (定义成函数)
  13. num_1=[1,2,13,5,8,9]
  14. def test(array):
  15. res =[]
  16. for i in num_1:
  17. res.append(i**2)
  18. return res
  19. end1 = test(num_1)
  20. end2 = test(num_1) # 以后用直接调用函数就可以了
  21. print('打印结果:',end1)
  22. print('打印结果:',end2)
  23.  
  24. 打印结果: [1, 4, 169, 25, 64, 81]
  25. 打印结果: [1, 4, 169, 25, 64, 81]
  26.  
  27. 提的需求就是功能,功能就要封装在函数里
  28. num_1=[1,2,13,5,8,9]
  29. def reduce_1(x): # 定义自减1函数
  30. return x-1
  31. def add_1(x): # 定义自增1函数
  32. return x+1
  33. def test(func,array):
  34. res =[]
  35. for i in num_1:
  36. ret=func(i)
  37. res.append(ret)
  38. return res
  39. print('自增1的结果:',test(add_1,num_1))
  40. print('自减1的结果:',test(reduce_1,num_1))
  41.  
  42. 自增1的结果: [2, 3, 14, 6, 9, 10]
  43. 自减1的结果: [0, 1, 12, 4, 7, 8]
  44.  
  45. # 终极版本 最简单的.使用匿名函数同样可以做到
  46. num_1=[1,2,13,5,8,9]
  47. def test(func,array):
  48. res =[]
  49. for i in num_1:
  50. ret=func(i)
  51. res.append(ret)
  52. return res
  53. print('自增1的结果:',test(lambda x:x+1,num_1))
  54. print('自减1的结果:',test(lambda x:x-1,num_1))
  55. print('平方的结果:',test(lambda x:x**2,num_1))
  56. print('除2的结果:',test(lambda x:x/2,num_1))
  57.  
  58. 自增1的结果: [2, 3, 14, 6, 9, 10]
  59. 自减1的结果: [0, 1, 12, 4, 7, 8]
  60. 平方的结果: [1, 4, 169, 25, 64, 81]
  61. 2的结果: [0.5, 1.0, 6.5, 2.5, 4.0, 4.5]
  62.  
  63. 内置函数map函数
  64. num_1=[1,2,13,5,8,9]
  65. def test(func,array):
  66. res =[]
  67. for i in num_1:
  68. ret=func(i)
  69. res.append(ret)
  70. return res
  71. print('匿名函数的处理结果:',test(lambda x:x+1,num_1))
  72.  
  73. ret = map(lambda x:x+1,num_1)
  74. print('内置函数map的处理结果:',list(ret))
  75.  
  76. 匿名函数的处理结果: [2, 3, 14, 6, 9, 10]
  77. 内置函数map的处理结果: [2, 3, 14, 6, 9, 10]

  78. map函数 还适用与自己设定的函数
  79. num_1=[1,2,13,5,8,9]
  80. def reduce_1(x): # 定义自减1函数
  81. return x-1
  82. def add_1(x): # 定义自增1函数
  83. return x+1
  84. def test(func,array):
  85. res =[]
  86. for i in num_1:
  87. ret=func(i)
  88. res.append(ret)
  89. return res
  90. ret = map(reduce_1,num_1)
  91. ret1 = map(add_1,num_1)
  92. print('map处理自减1的结果:',list(ret))
  93. print('map处理自加1的结果:',list(ret1))
  94.  
  95. map处理自减1的结果: [0, 1, 12, 4, 7, 8]
  96. map处理自加1的结果: [2, 3, 14, 6, 9, 10]

  97. map还以可以迭代其他对象,
  98. msg ='majunnihao' #(例如小写转换成大写)
  99. t = map(lambda x:x.upper(),msg)
  100. print(list(t))
  101.  
  102. ['M', 'A', 'J', 'U', 'N', 'N', 'I', 'H', 'A', 'O']

py-day3-6 python map函数的更多相关文章

  1. python map函数(23)

    截至到目前为止,其实我们已经接触了不少的python内置函数,而map函数也是其中之一,map函数是根据指定函数对指定序列做映射,在开发中使用map函数也是有效提高程序运行效率的办法之一. 一.语法定 ...

  2. python map函数

    map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例如,对于li ...

  3. python map函数 reduce函数

    Python中map()函数浅析 函数式编程: 更好的描述问题 map函数 怎么理解当传入多个参数list时,map如何运作:    abc函数第一次传入的数据时 (11,44,77),然后(22,5 ...

  4. python——map()函数

    描述 map() 会根据提供的函数对指定序列做映射. 第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表. 语法 m ...

  5. python map函数的使用

    python2 中的map函数返回列表 python3 中的map函数返回迭代器 >>>def square(x) : # 计算平方数 ... return x ** 2 ... & ...

  6. python map函数、filter函数、reduce函数

    1.map函数:map(func,可迭代对象): ①func可以是自定义的函数,也可以是功能简单的匿名函数(通过lambda定义) ②处理逻辑:表示将传入的可迭代对象依次循环,将每个元素按照传入的fu ...

  7. day16 Python map函数

    num_l=[1,2,10,5,3,7] #lambda x:x+1 # def add_one(x): # return x+1 #lambda x:x+1 # def reduce_one(x): ...

  8. Python map,filter,reduce函数

    # -*- coding:utf-8 -*- #定义一个自己的map函数list_list = [1,2,4,8,16] def my_map(func,iterable): my_list = [] ...

  9. python filter函数(40)

    一.filter函数简介 filter函数主要用来筛选数据,过滤掉不符合条件的元素,并返回一个迭代器对象,如果要转换为列表list或者元祖tuple,可以使用内置函数list() 或者内置函数tupl ...

随机推荐

  1. 《JavaScript Dom 编程艺术》读书笔记-第11章

    本章简单介绍了HTML5,并推荐了一个好工具Modernizr,用于检测浏览器可能支持的各种特性. HTML5的新特性包括: 可以用来在文档中绘制矢量及位图的<canvas>元素: 可以在 ...

  2. effective_java 第34条:用接口模拟可伸缩的枚举

    例如: /** * 加减乘除枚举 * Created by yulinfeng on 8/20/17. */ public enum Operation { PLUS { double apply(d ...

  3. CSS规范—分类方法(NEC规范学习笔记)

    一.CSS文件的分类和引用顺序 Css按照性质和用途,将Css文件分成“公共型样式”.“特殊型样式”.“皮肤型样式”,并以此顺序引用,有需要可以添加版本号 1.公共型样式:包含以下几个部分 标签的重置 ...

  4. Shell 有类型变量

    1.通过 declare 和 typeset 命令 declare 和 typeset 两者等价 declare 和 typeset 都是用来定义变量类型的 下面以 declare 进行总结 2.de ...

  5. JAVA 集合 按照某个字段(依据一定条件)进行分组

    由于数据不能够在本地化实现, 无法通过sql语句得到对应的结果,小编只好在业务层处理.通过调用接口得到集合,拿到集合后,通过年来分组,以此来达到对应的Map集合... 在这里小编给大家提供一个封装了一 ...

  6. ubuntu编译安装opencv

    简易安装opencv2: conda install --channel https://conda.anaconda.org/menpo opencv 或: sudo apt-get install ...

  7. 对lua中__newindex的理解

    阅读了文章后用流程图来总结一下 __newindex的规则: a.如果__newindex是一个函数,则在给table不存在的字段赋值时,会调用这个函数.b.如果__newindex是一个table, ...

  8. python 10

    不想写了,用一下强大的copy功能吧!! (转自:我的同桌)

  9. 微信小程序上传后发布或者体验版测试无数据解决办法

    在做微信小程序开发的过程中,发现小程序在本地调用接口的数据都显示,但是上传之后,发现手机体验没有数据.以下为解决办法: 1.先清除缓存试试. 2.打开微信小程序工具右上角的详情——项目设置,将“不校验 ...

  10. 锋利的jQuery初学(5)

    层级选择器: 层级选择器 符号 解释 使用 空格 后代选择器 $("div p").css("","") + 紧邻选择器 $("d ...