【Rollo的Python之路】python 内置函数

内置函数:

abs()    divmod()    input()    open()    staticmethod()
all()    enumerate()    int()    ord()    str()
any()    eval()    isinstance()    pow()    sum()
basestring()    execfile()    issubclass()    print()    super()
bin()    file()    iter()    property()    tuple()
bool()    filter()    len()    range()    type()
bytearray()    float()    list()    raw_input()    unichr()
callable()    format()    locals()    reduce()    unicode()
chr()    frozenset()    long()    reload()    vars()
classmethod()    getattr()    map()    repr()    xrange()
cmp()    globals()    max()    reverse()    zip()
compile()    hasattr()    memoryview()    round()    __import__()
complex()    hash()    min()    set()    
delattr()    help()    next()    setattr()    
dict()    hex()    object()    slice()    
dir()    id()    oct()    sorted()    exec 内置表达式

1.0 map()

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

def square(x):
    return x**2

s = map(square,[1,2,3,4,5])

print(s)
print(list(s))

#执行结果:

<map object at 0x0000000002119B70>
[1, 4, 9, 16, 25]

以上的代码还可以直接用lambda:

map(lambda x:x**2,[1,2,3,4,5])

map()可以对列表进行处理

str = ["a","b","c","d"]
def function2(x):
    return x + "Rollo"

result = map(function2,str)

print(result)
print(list(result))

#执行结果:

<map object at 0x0000000002139B70>
['aRollo', 'bRollo', 'cRollo', 'dRollo']

提供了两个列表,对相同位置的列表数据进行相加

map(lambda x,y:x + y,[1,2,3,4,5],[6,7,8,9,10])

map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回。

map(str,[1,2,3,4,6,7,8])

把这个list所有数字转为字符串

2.0 filter():作用就是利用逻辑数字过滤不要的元素,生成先的列表

filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。

该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表中。

注意: Pyhton2.7 返回列表,Python3.x 返回迭代器对象

方法:

filter(function,Iterable)
str = ["a","b","c","d"]
def function_test(x):
    if x != "a":
        return x

p = filter(function_test,str)

print(p)
print(list(p))

#执行结果:

<filter object at 0x0000000001DE9B70>
['b', 'c', 'd']

3.0 reduce():reduce的结果就是一个值

reduce(function, iterable[, initializer])

reduce() 函数会对参数序列中元素进行累积。

函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果。

from functools import reduce

def addition(x,y):
    return x + y

print(reduce(addition,range(1,10)))

#执行结果:

45

效果就是这样的:

reduce(addition, [x1, x2, x3, x4]) = addition(addition(addition(x1, x2), x3), x4)

当然上面的代码直接用sum()就可以

t = sum((1,2,3,4,5,6,7,8,9))
print(t)

但是如果要把序列[1, 3, 5, 7, 9]变换成整数13579,reduce就可以派上用场:

from functools import reduce

num = [1,3,5,7,9]

def fn(x,y):
    return x*10 + y
reduce(fn,num)

print(reduce(fn,num))

4.0 lambda() :

1.0 关键字lambda表示匿名函数,冒号前面的x表示函数参数。

2.0 lambda只是一个表达式,只是能一行代码,不能用return,返回值就是他本身。函数体比def简单很多。

3.0 用匿名函数有个好处,因为函数没有名字,不必担心函数名冲突。此外,匿名函数也是一个函数对象,也可以把匿名函数赋值给一个变量,再利用变量来调用该函数:

4.0 lambda函数拥有自己的命名空间,且不能访问自有参数列表之外或全局命名空间里的参数。

map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9])
[1, 4, 9, 16, 25, 36, 49, 64, 81]
def build(x, y):
    return lambda: x * x + y * y

5.0 sorted():对所以可迭代对象进行排序。

排序也是在程序中经常用到的算法。无论使用冒泡排序还是快速排序,排序的核心是比较两个元素的大小。如果是数字,我们可以直接比较,但如果是字符串或者两个dict呢?直接比较数学上的大小是没有意义的,因此,比较的过程必须通过函数抽象出来。通常规定,对于两个元素xy,如果认为x < y,则返回-1,如果认为x == y,则返回0,如果认为x > y,则返回1,这样,排序算法就不用关心具体的比较过程,而是根据比较结果直接排序

t = sorted([36,25,78,1,96,35])
print(t)

#执行结果:

[1, 25, 35, 36, 78, 96]

5.0 abs() :

函数返回数字的绝对值。

a = abs(-45)
b = abs(1935.35)
c = abs(19654L)  #pyhon3不能加字母

print(a)
print(b)
print(c)

#执行结果:

45
1935.35
报错

6.0 divmod()

猜你喜欢

转载自www.cnblogs.com/rollost/p/10803643.html
今日推荐