python map()函数

作者: 盖码范 | 来源:发表于2019-07-18 15:13 被阅读3次

描述

map() 会根据提供的函数对指定序列做映射。

第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。

语法

map()函数语法:

map(function, iterable, ...)

参数

  • function -- 函数
  • iterable -- 一个或多个序列

返回值

Python 2.x 返回列表。
Python 3.x 返回迭代器。

题目

请将列表[1,2,3,4,5]使用python方法转变成[1,4,9,16,25]。然后提取大于10的数,最终输出[16,25]

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

方法一
b = list()
c = list()
for i in a:
    b.append(i*i)
b
Out[19]: [1, 4, 9, 16, 25]  # 第一步

for i in b:
    if i > 10:
        c.append(i)
c
Out[5]: [16, 25] # 第二步


方法二
list(map(lambda x:x*x, a)) # 第一步
Out[16]: [1, 4, 9, 16, 25]

[i for i in map(lambda x:x*x, a) if i > 10] 
Out[6]: [16, 25] # 第二步

方法三
list(map(lambda x:pow(x,2), a)) # 第一步
Out[17]: [1, 4, 9, 16, 25]

[i for i in map(lambda x:pow(x,2), a) if i > 10] 
Out[7]: [16, 25] # 第二步

相关文章

  • Python高阶函数学习笔记

    python中的高阶函数是指能够接收函数作为参数的函数 python中map()函数map()是 Python 内...

  • Lesson 025 —— python 常用函数与深浅拷贝

    Lesson 025 —— python 常用函数与深浅拷贝 常用函数 map()map()是 Python 内置...

  • Python学习记录

    基本语法 Python lambda介绍 常用函数 python map( )函数用法map将传入的函数f依次作用...

  • python——list(map())函数的用法

    版本:python3.7 map()函数## map()是 Python 内置的高阶函数,它接收一个函数 f 和一...

  • Python的高级函数

    Python的高级函数 1. map函数 map(func, *itertables) 对itertables中...

  • map/reduce

    Python内建了map()和reduce()函数。 1、map()函数map()函数接收两个参数,一个是函数,一...

  • python 中的map(转载)

    1 map()函数的简介以及语法: map是python内置函数,会根据提供的函数对指定的序列做映射。 map()...

  • Python map函数

    python3中返回迭代器: 函数map lambda map

  • Python3 小技巧

    集合操作 字典操作 两个字典 相交、合并、相差 Python 映射 Python 内置函数 map();map()...

  • Python 18:map/reduce

    python内置可map()和reduce()函数。我们先看map。map()函数接收两个参数,一个是函数,一个是...

网友评论

    本文标题:python map()函数

    本文链接:https://www.haomeiwen.com/subject/tejllctx.html