Python中map函数
1.简介
python 提供内置函数map(), 接收两个参数,一个是函数,一个是序列,
map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返
回。
例如:
(1)对于list [1, 2, 3, 4, 5, 6, 7, 8, 9]
如果希望把list的每个元素都作平方,就可以用map()函数:
因此,我们只需要传入函数f(x)=x*x,就可以利用map()函数完成这个计算:
def f(x):
return x * x
print(list(map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])))
输出结果:
[1, 4, 9, 10, 25, 36, 49, 64, 81]
也可以结合lambda这样写:
print map(lambda x: x**2, list)
注意:map()函数不改变原有的 list,而是返回一个新的 list。
(2)list中字符串转换成int
list2 = ['1', '2', '3', '4', '5']
print(list(map(lambda x:int (x), list2)))
输出结果:
[1, 2, 3, 4, 5]
利用map()函数,可以把一个 list 转换为另一个 list,只需要传入转换函数。
由于list包含的元素可以是任何类型,因此,map() 不仅仅可以处理只包含数值的 list,事实上它可以处理包含任意类型的 list,只要传入的函数f可以处理这种数据类型。
2.注意(Python3中map函数的问题):
在Python2中map函数会返回一个list列表,如代码:
>>> def f(x, y): return (x, y) >>> l1 = [ 0, 1, 2, 3, 4, 5, 6 ] >>> l2 = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]
返回结果如下:
>>> map(f, l1, l2) [(0, 'Sun'), (1, 'Mon'), (2, 'Tue'), (3, 'Wed'), (4, 'Thu'), (5, 'Fri'), (6, 'Sat')]
但是,在Python3中返回结果如下:
>>> map(f1, l1, l2) <map object at 0x00000000021DA860>
如果想要得到Python2的那种结果,即返回list列表,那么必须用list作用于map,如下:
>>> list(map(f1, l1, l2)) [(0, 'Sun'), (1, 'Mon'), (2, 'Tue'), (3, 'Wed'), (4, 'Thu'), (5, 'Fri'), (6, 'Sat')]
3.例子(Python3实现):
假设用户输入的英文名字不规范,没有按照首字母大写,后续字母小写的规则,请利用map()函数,把一个list(包含若干不规范的英文名字)变成一个包含规范英文名字的list:
方法1:
def format_name(str):
str1 = str[0:1].upper() + str[1:].lower()
return str1 name_list = ['BOB', 'tom', 'IVy']
print(list(map(format_name, name_list)))
方法2:
def format_name1(str):
str1 = str.capitalize()
return str1 name_list = ['BOB', 'tom', 'IVy']
print(list(map(format_name1, name_list)))
方法3:
def format_name1(str):
str1 = str.title()
return str1 name_list = ['BOB', 'tom', 'IVy']
print(list(map(format_name1, name_list)))
输出结果:
['Bob', 'Tom', 'Ivy']
Python中map函数的更多相关文章
- Python中map()函数浅析
MapReduce的设计灵感来自于函数式编程,这里不打算提MapReduce,就拿python中的map()函数来学习一下. 文档中的介绍在这里: map(function, iterable, .. ...
- python中map()函数的用法讲解
map函数的原型是map(function, iterable, -),它的返回结果是一个列表. 参数function传的是一个函数名,可以是python内置的,也可以是自定义的. 参数iterabl ...
- python中map()函数
map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. map()是 Python 内 ...
- python中map函数和reduce函数的区别
①从参数方面来讲:map()函数: map()包含两个参数,第一个是参数是一个函数,第二个是序列(列表或元组).其中,函数(即map的第一个参数位置的函数)可以接收一个或多个参数.reduce()函数 ...
- python中map函数的用法
map函数类似一个生成器 具体用例如下: def add(x): a =[,,] b = map(add,[,,]) print( list(map(add,[,,])) ) print(b,type ...
- 【转】Python 中map、reduce、filter函数
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- Python 中的函数
学了 Python 中的数据类型,语句,接下来就来说一下 Python 中的函数,函数是结构化编程的核心.我们使用函数可以增加程序的可读性.自定义函数时使用关键字def 函数由多条语句组成.在定义函数 ...
- Python中针对函数处理的特殊方法
Python中针对函数处理的特殊方法 很多语言都提供了对参数或变量进行处理的机制,作为灵活的Python,提供了一些针对函数处理的特殊方法 filter(function, sequence):对se ...
- [19/10/13-星期日] Python中的函数
一.函数 # 第五章 函数 ## 函数简介(function) - 函数也是一个对象 - 对象是内存中专门用来存储数据的一块区域 - 函数可以用来保存一些可执行的代码,并且可以在需要时,对这些语句进行 ...
随机推荐
- Task任务的屏障机制
Barrier 是 .Net 提供的一直并发的机制,它允许多个任务同步他们不同阶段的并发工作. 这里的关键点是[多个任务]和[不同阶段]. 假设有4个相同的任务(Task),每个任务都有4个阶段(Ph ...
- UTC时间戳转为时间
/// <summary> /// 将UTC时间转化DateTime时间 /// </summary> /// <returns></returns> ...
- 挂在光盘出现写保护mount: block device /dev/sr0 is write-protected, mounting read-only
https://blog.csdn.net/yueludanfeng/article/details/60339688
- noip2017逛公园
题解: 之前知道正解并没有写过.. #include <bits/stdc++.h> using namespace std; #define rint register int #def ...
- HDU2853 Assignment KM
原文链接http://www.cnblogs.com/zhouzhendong/p/8284105.html 题目传送门 - HDU2853 题意概括 (来自谷歌翻译) 题解 这是一道好题. 我们首先 ...
- Servlet解决中文乱码问题
request.setCharacterEncoding("UTF-8"); 并且把这句话放在request.getParameter()之前
- 006 使用SpringMVC开发restful API四--用户信息的修复与删除,重在注解的定义
一:任务 1.任务 常用的验证注解 自定义返回消息 自定义校验注解 二:Hibernate Validator 1.常见的校验注解 2.程序 测试类 /** * @throws Exception * ...
- int和intege相比
int i = 100; Integer i1 = 100; Integer i2 = new Integer(100); system.out.println(i == i1);true syste ...
- Python编程基础(一)
1.Python中的变量赋值不需要类型声明 2.等号(=)用来给变量赋值 3.字符串拼接用 “+” 号 temp=‘123’ print('temp的值是%s'%temp) #整数和字符创的转换, ...
- react-antd 按需加载报错
基于create-react-app 搭建的 react 项目 引入 antd UI 配置按需加载 但是报一下错误 .翻译过了一下 是内嵌JavaScript选项没有开启什么的 大白话就是 les ...