Python学习笔记——内置函数
来源:互联网 发布:哈尔滨哪里有mac专柜 编辑:程序博客网 时间:2024/04/30 01:14
- apply(function,args[,keywords])
The function argument must be a callable object (a user-defined or built-infunction or method, or a class object) and theargs argument must be asequence. Thefunction is called withargs as the argument list; the numberof arguments is the length of the tuple. If the optionalkeywords argument ispresent, it must be a dictionary whose keys are strings. It specifies keywordarguments to be added to the end of the argument list. Callingapply() isdifferent from just callingfunction(args), since in that case there isalways exactly one argument. The use ofapply() is equivalent tofunction(*args,**keywords).
map(function, iterable,...)
Apply function to every item of iterable and return a list of the results.If additionaliterable arguments are passed,function must take that manyarguments and is applied to the items from all iterables in parallel. If oneiterable is shorter than another it is assumed to be extended withNoneitems. Iffunction isNone, the identity function is assumed; if thereare multiple arguments,map() returns a list consisting of tuplescontaining the corresponding items from all iterables (a kind of transposeoperation). Theiterable arguments may be a sequence or any iterable object;the result is always a list.
apply与map的区别,map的参数iterable是依次作为参数,调用function,然后各自的返回值作为一个list,作为map方法的返回值。而apply的args可以是一个list,作为function的参数,一次性的调用function。而apply也不同于function(agrs),apply可以是以字典作为不定参数的版本,相当于function(*args,**keywords)
a = [1,2,3,4,5,6,7,8,9,10]def calc(*arg): return 2*argdef calc2(arg): return 2*argdef calc3(arg, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9): return 2*arg#map是将参数a这个序列中的每个成员,以此作为参数,调用calc函数,将函数的返回值,依次生成一个list作为map的返回值#将参数解释为单个数字,返回的也是数字,map的返回值就是一系列数字组成的列表print map(calc2, a)#将参数设置为不定参数,输入就解释为元组,calc的返回值就是就是元组,map的返回值就是元组的列表print map(calc, a)#apply()是将参数a作为一个整体,调用calc,所以calc必须是10个参数(如calc的版本),或者使用不定参数(参数被解释为元组,如calc3的版本)print apply(calc, a)print apply(calc3, a)
执行结果
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10)]
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
2
- Python学习笔记——内置函数
- [Python学习笔记][Python内置函数]
- python 学习笔记之内置函数
- python学习笔记2---内置函数
- python内置函数笔记
- Python两个内置函数——locals 和globals (学习笔记)
- Python两个内置函数——locals 和globals (学习笔记)
- Python学习笔记2(小甲鱼)—— 内置函数BIF
- Python两个内置函数——locals 和globals (学习笔记)
- Python两个内置函数——locals 和globals (学习笔记)
- Python两个内置函数——locals 和globals (学习笔记)
- Python学习:内置函数
- python内置函数学习
- Python内置函数学习
- Python学习-内置函数
- Python学习笔记(14)--python内置函数大全
- Python学习笔记(三)函数初识和内置函数
- Python学习笔记(三):常用内置函数学习
- Cocos2dx v3.2骨骼动画加载学习
- 《世界上最远的距离》
- tomcat 调用 glassfish4 中 EJB 必备jar包
- Android 调试打印信息的方法
- HDU 3682 水模拟
- Python学习笔记——内置函数
- H264 帧边界识别简介
- RGB转换成Lab具体算法
- libevent源码深度剖析三
- 输入的那些函数
- MySQL数据库导入导出
- Effective C++ 读书笔记 条款06:若不想使用编译器自动生成的函数,就该明确拒绝
- Android Makefile 文件讲解
- easyui datagrid 日间格化(JS 日期时间本地化显示)