赛博红兔的科技博客

CyberHongTu shares news, insights, and musings on fascinating technology subjects.


和我一起玩Python:19. 函数(后篇)

我们接着讲Python里的lambda函数概念。Lambda函数就是一些小巧的、匿名的、用一行代码定义的函数,主要用在只需要用一次的场合。它的语法是lambda 参数: 表达式。通过例子我们看看怎么用它来做一些简单操作,还有怎么定义高阶函数。有个实用的练习是,用sorted()函数来按每个单词的最后一个字母排序,这里会用到一个关键字key。最后,我们展示了lambda函数怎么和map()、filter()这些函数配合使用,进行列表操作,比如翻倍值和过滤掉特定的数字。

built-in functions list

FunctionDescription
abs()Returns the absolute value of a number
all()Returns True if all items in an iterable object are true
any()Returns True if any item in an iterable object is true
ascii()Returns a readable version of an object. Replaces none-ascii characters with escape character
bin()Returns the binary version of a number
bool()Returns the boolean value of the specified object
bytearray()Returns an array of bytes
bytes()Returns a bytes object
callable()Returns True if the specified object is callable, otherwise False
chr()Returns a character from the specified Unicode code.
classmethod()Converts a method into a class method
compile()Returns the specified source as an object, ready to be executed
complex()Returns a complex number
delattr()Deletes the specified attribute (property or method) from the specified object
dict()Returns a dictionary (Array)
dir()Returns a list of the specified object’s properties and methods
divmod()Returns the quotient and the remainder when argument1 is divided by argument2
enumerate()Takes a collection (e.g. a tuple) and returns it as an enumerate object
eval()Evaluates and executes an expression
exec()Executes the specified code (or object)
filter()Use a filter function to exclude items in an iterable object
float()Returns a floating point number
format()Formats a specified value
frozenset()Returns a frozenset object
getattr()Returns the value of the specified attribute (property or method)
globals()Returns the current global symbol table as a dictionary
hasattr()Returns True if the specified object has the specified attribute (property/method)
hash()Returns the hash value of a specified object
help()Executes the built-in help system
hex()Converts a number into a hexadecimal value
id()Returns the id of an object
input()Allowing user input
int()Returns an integer number
isinstance()Returns True if a specified object is an instance of a specified object
issubclass()Returns True if a specified class is a subclass of a specified object
iter()Returns an iterator object
len()Returns the length of an object
list()Returns a list
locals()Returns an updated dictionary of the current local symbol table
map()Returns the specified iterator with the specified function applied to each item
max()Returns the largest item in an iterable
memoryview()Returns a memory view object
min()Returns the smallest item in an iterable
next()Returns the next item in an iterable
object()Returns a new object
oct()Converts a number into an octal
open()Opens a file and returns a file object
ord()Convert an integer representing the Unicode of the specified character
pow()Returns the value of x to the power of y
print()Prints to the standard output device
property()Gets, sets, deletes a property
range()Returns a sequence of numbers, starting from 0 and increments by 1 (by default)
repr()Returns a readable version of an object
reversed()Returns a reversed iterator
round()Rounds a numbers
set()Returns a new set object
setattr()Sets an attribute (property/method) of an object
slice()Returns a slice object
sorted()Returns a sorted list
staticmethod()Converts a method into a static method
str()Returns a string object
sum()Sums the items of an iterator
super()Returns an object that represents the parent class
tuple()Returns a tuple
type()Returns the type of an object
vars()Returns the __dict__ property of an object
zip()Returns an iterator, from two or more iterators

内置函数列表

函数描述
abs()返回数字的绝对值
all()如果可迭代对象中所有项为真,则返回True
any()如果可迭代对象中任何一项为真,则返回True
ascii()返回对象的可读版本。用转义字符替换非ascii字符
bin()返回数字的二进制版本
bool()返回指定对象的布尔值
bytearray()返回字节的数组
bytes()返回一个字节对象
callable()如果指定的对象可调用,则返回True,否则返回False
chr()返回指定Unicode码的字符
classmethod()将方法转换为类方法
compile()返回准备执行的指定源的对象
complex()返回一个复数
delattr()从指定对象中删除指定属性(属性或方法)
dict()返回一个字典(数组)
dir()返回指定对象的属性和方法的列表
divmod()当参数1被参数2除时,返回商和余数
enumerate()获取一个集合(例如元组)并返回它作为一个枚举对象
eval()评估并执行表达式
exec()执行指定的代码(或对象)
filter()使用过滤函数排除可迭代对象中的项
float()返回一个浮点数
format()格式化指定的值
frozenset()返回一个frozenset对象
getattr()返回指定属性(属性或方法)的值
globals()以字典形式返回当前的全局符号表
hasattr()如果指定的对象具有指定的属性(属性/方法),则返回True
hash()返回指定对象的哈希值
help()执行内置的帮助系统
hex()将数字转换为十六进制值
id()返回一个对象的id
input()允许用户输入
int()返回一个整数
isinstance()如果指定的对象是指定对象的实例,则返回True
issubclass()如果指定的类是指定对象的子类,则返回True
iter()返回一个迭代器对象
len()返回对象的长度
list()返回一个列表
locals()返回当前本地符号表的更新字典
map()返回应用了指定函数到每个项的指定迭代器
max()返回可迭代对象中的最大项
memoryview()返回一个内存视图对象
min()返回可迭代对象中的最小项
next()返回可迭代对象中的下一个项
object()返回一个新对象
oct()将数字转换为八进制
open()打开一个文件并返回一个文件对象
ord()将代表指定字符的Unicode的整数转换
函数描述
pow()返回x的y次幂的值
print()打印到标准输出设备
property()获取,设置,删除属性
range()返回一个数字序列,从0开始,以1(默认)递增
repr()返回对象的可读版本
reversed()返回一个反向迭代器
round()对数字进行四舍五入
set()返回一个新的集合对象
setattr()设置对象的属性(属性/方法)
slice()返回一个切片对象
sorted()返回一个排序的列表
staticmethod()将方法转换为静态方法
str()返回一个字符串对象
sum()对迭代器的项求和
super()返回代表父类的对象
tuple()返回一个元组
type()返回对象的类型
vars()返回对象的__dict__属性
zip()从两个或更多的迭代器中返回一个迭代器


Leave a comment