python内建函数是什么_Python 的内建函数

58561-106.jpg

Python内建了很多有用的函数,我们可以直接调用。

要调用一个函数,需要知道函数的名称和参数,最好的方法就是去看官方文档,并去查看源码来查看他的是实现原理以便于更好得理解。

这里介绍一些常用的内置函数。

abs函数

绝对值的函数

>>> b = abs(-12)

>>> b

12

>>> b = abs(12)

>>> b

12

>>>

可以在交互式命令行通过help(内置函数)来查看的方法和参数。

>>> help(abs)

Help on built-in function abs in module __builtin__:

abs(...)

abs(number) -> number

Return the absolute value of the argument.

>>>

max函数和min函数>>> b = max(1,2,3,4,5,6)

>>> b

6

>>> b = min(1,2,3,4,5,6)

>>> b

1

>>>

数据类型转换的函数>>> type('3')

>>> b = int('3')

>>> b

3

>>> type(b)

>>>

>>> b = float(3.1415926)

>>> b

3.1415926

>>> b = float(3)

>>>b

3.0

>>> type(3)

>>> b = str(3)

>>> b

'3'

>>> type(b)

>>>

>>> b = bool(0)

>>> b

False

>>> b = bool(1)

>>> b

True

>>> b = bool('')

>>> b

False

>>> b = bool('a')

>>> b

True

>>>

cmp函数

如果x < y ,返回负数;x == y, 返回0;x > y,返回正数

>>> cmp(1,2)

-1

>>> cmp(2,1)

1

>>>

dir([object])

1、不带参数时,返回当前范围内的变量、方法和定义的类型列表;

2、带参数时,返回参数的属性、方法列表。

3、如果参数包含方法__dir__(),该方法将被调用。当参数为实例时。

4、如果参数不包含__dir__(),该方法将最大限度地收集参数信息

>>> dir()

['__builtins__', '__doc__', '__name__', '__package__', 'b']

>>> dir(str)

['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

>>>

eval函数

eval()函数十分强大,官方demo解释为:将字符串str当成有效的表达式来求值并返回计算结果。

>>> a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"

>>> type(a)

>>> b = eval(a)

>>> b

[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]

>>> type(b)

>>>

>>> a = "{'hello':'world', 1:2}"

>>> type(a)

>>> b = eval(a)

>>> b

{1: 2, 'hello': 'world'}

>>> type(b)

>>>

>>> a = '(1,2,3,4,5,6,7,8,9)'

>>> type(a)

>>> b = eval(a)

>>> b

(1, 2, 3, 4, 5, 6, 7, 8, 9)

>>> type(b)

>>>

虽然强大,但是安全性不高

filter(function, iterable)

构造一个序列,等价于[ item for item in iterable if function(item)]

1、参数function:返回值为True或False的函数,可以为None

2、参数iterable:序列或可迭代对象

filter函数会对指定序列执行过滤操作。

filter函数的定义:

filter(function or None, sequence) -> list, tuple, or string

function是一个谓词函数,接受一个参数,返回布尔值True或False。

filter函数会对序列参数sequence中的每个元素调用function函数,最后返回的结果包含调用结果为True的元素。

返回值的类型和参数sequence的类型相同

>>> def fun(num):

if 3

return num

>>> lists = [12,3,4,56,66,774,5,2]

>>> result = filter(fun,lists)

>>> result

[4, 5]

>>>

repr()函数

将一个对象变幻为可打印的格式

>>> s = 'hello world'

>>> b = repr(s)

>>> b

"'hello world'"

>>>

python str和repr的区别:

尽管str(),repr()和运算在特性和功能方面都非常相似,事实上repr()和做的是完全一样的事情,它们返回的是一个对象的“官方”字符串表示,也就是说绝大多数情况下可以通过求值运算(使用内建函数eval())重新得到该对象。

但str()则有所不同,str()致力于生成一个对象的可读性好的字符串表示,它的返回结果通常无法用于eval()求值,但很适合用于print语句输出。需要再次提醒的是,并不是所有repr()返回的字符串都能够用 eval()内建函数得到原来的对象。 也就是说 repr() 输出对 Python比较友好,而str()的输出对用户比较友好。

虽然如此,很多情况下这三者的输出仍然都是完全一样的。 大家可以看下下面的代码,来进行对比:

>>> s = 'Hello, world.'

>>> str(s)

'Hello, world.'

>>> repr(s)

"'Hello, world.'"

>>> str(0.1)

'0.1'

>>> repr(0.1)

'0.10000000000000001'

>>> x = 10 * 3.25

>>> y = 200 * 200

>>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'

>>> print s

The value of x is 32.5, and y is 40000...

>>> # The repr() of a string adds string quotes and backslashes:

... hello = 'hello, world\n'

>>> hellos = repr(hello)

>>> print hellos

'hello, world\n'

>>> # The argument to repr() may be any Python object:

... repr((x, y, ('spam', 'eggs')))

"(32.5, 40000, ('spam', 'eggs'))"

super(type[, object-or-type])函数

普通继承

class FooParent(object):

def __init__(self):

self.parent = 'I\'m the parent.'

print 'Parent'

def bar(self,message):

print message, 'from Parent'

class FooChild(FooParent):

def __init__(self):

FooParent.__init__(self)

print 'Child'

def bar(self,message):

FooParent.bar(self,message)

print 'Child bar function.'

print self.parent

if __name__=='__main__':

fooChild = FooChild()

fooChild.bar('HelloWorld')

super继承

class FooParent(object):

def __init__(self):

self.parent = 'I\'m the parent.'

print 'Parent'

def bar(self,message):

print message,'from Parent'

class FooChild(FooParent):

def __init__(self):

super(FooChild,self).__init__()

print 'Child'

def bar(self,message):

super(FooChild, self).bar(message)

print 'Child bar fuction'

print self.parent

if __name__ == '__main__':

fooChild = FooChild()

fooChild.bar('HelloWorld')

程序运行结果相同,为:

Parent

Child

HelloWorld from Parent

Child bar fuction

I'm the parent.

从运行结果上看,普通继承和super继承是一样的。但是其实它们的内部运行机制不一样,这一点在多重继承时体现得很明显。在super机制里可以保证公共父类仅被执行一次,至于执行的顺序,是按照mro进行的(E.mro)。

注意super继承只能用于新式类,用于经典类时就会报错。

新式类:必须有继承的类,如果没什么想继承的,那就继承object

经典类:没有父类,如果此时调用super就会出现错误:『super() argument 1 must be type, not classobj

isinstance()函数

isinstance(object, classinfo)

判断实例是否是这个类或者object是变量

classinfo 是类型(tuple,dict,int,float)

判断变量是否是这个类型

>>> num = 10 # 定义一个变量为整数10

>>> def function(): # 定义一个叫function的函数,不做任何操作

pass

>>> class test(): # 定义一个叫test的类,不做任何操作

pass

>>> isinstance(num,int) # 判断num是不是整型

True

>>> isinstance(num,(int,str)) # 判断num是不是整型或者字符串

True

>>> isinstance(num,(float,str)) # 判断num是不是整型或者浮点型

False

>>> isinstance(function,object) # 判断function是不是一个对象

True

>>> isinstance(function,int) # 判断function是不是一个整型

False

>>> isinstance(test,object) # 判断test是不是一个对象

True

>>> isinstance(test,str) # 判断test是不是一个字符串

False

>>>

map()函数

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

map函数的定义:

map(function, sequence[, sequence, ...]) -> list

通过定义可以看到,这个函数的第一个参数是一个函数,剩下的参数是一个或多个序列,返回值是一个集合。

function可以理解为是一个一对一或多对一函数,map的作用是以参数序列中的每一个元素调用function函数,返回包含每次function函数返回值的list。

比如要对一个序列中的每个元素进行平方运算:

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

返回结果为:

>>>[1, 4, 9, 16, 25]

在参数存在多个序列时,会依次以每个序列中相同位置的元素做参数调用function函数。

比如要对两个序列中的元素依次求和。

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

map返回的list中第一个元素为,参数序列1的第一个元素加参数序列2中的第一个元素(1 + 2),

list中的第二个元素为,参数序列1中的第二个元素加参数序列2中的第二个元素(3 + 4),

依次类推,最后的返回结果为:

>>>[3, 7, 11, 15, 19]

要注意function函数的参数数量,要和map中提供的集合数量相匹配。

如果集合长度不相等,会以最小长度对所有集合进行截取。

当函数为None时,操作和zip相似:

>>>map(None, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])

返回结果为:

>>>[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

reduce()函数

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

reduce函数的定义:

reduce(function, sequence[, initial]) -> value

function参数是一个有两个参数的函数,reduce依次从sequence中取一个元素,和上一次调用function的结果做参数再次调用function。

第一次调用function时,如果提供initial参数,会以sequence中的第一个元素和initial作为参数调用function,否则会以序列sequence中的前两个元素做参数调用function。

>>>reduce(lambda x, y: x + y, [2, 3, 4, 5, 6], 1) # ( (((((1+2)+3)+4)+5)+6) )

>>>21

>>>reduce(lambda x, y: x + y, [2, 3, 4, 5, 6])

>>>20

注意function函数不能为None。

zip()函数

zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表

>>> x = [1, 2, 3]

>>> y = [4, 5, 6]

>>> z = [7,8,9]

>>> xyz = zip(x,y,z)

>>> xyz

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

>>>

如果集合长度不相等,会以最小长度对所有集合进行截取

>>> x = [1, 2, 3]

>>> y = [4, 5, 6, 7]

>>> xy = zip(x, y)

>>> xy

[(1, 4), (2, 5), (3, 6)]

当只有一个参数时

>>> x = [1, 2, 3]

>>> x = zip(x)

>>> x

[(1,), (2,), (3,)]

当没有参数时

>>> x = zip()

>>> x

[]

>>>

来看这一个例子:

>>> x = [1, 2, 3]

>>> y = [4, 5, 6]

>>> z = [7, 8, 9]

>>> xyz = zip(x,y,z)

>>> xyz

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

>>> u = zip(*xyz)

>>> u

[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

>>>

一般认为这是一个unzip的过程,它的运行机制是这样的:

在运行zip(xyz)之前,xyz的值是:[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

那么,zip(xyz) 等价于 zip((1, 4, 7), (2, 5, 8), (3, 6, 9))

所以,运行结果是:[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

注:在函数调用中使用*list/tuple的方式表示将list/tuple分开,作为位置参数传递给对应函数(前提是对应函数支持不定个数的位置参数)

又一个例子:

>>> x = [1,2,3]

>>> r = zip(*[x]*3)

>>> r

[(1, 1, 1), (2, 2, 2), (3, 3, 3)]

>>>

它的运行机制是这样的:

[x]生成一个列表的列表,它只有一个元素x

[x] * 3生成一个列表的列表,它有3个元素,[x, x, x]

zip(* [x] * 3)的意思就明确了,zip(x, x, x)

sort()和sorted()函数

sort(list[cmp=None[, key=None[, reverse=False]]])

sorted(iterable[,cmp=None,[,key=None[,reverse=True]]])

两者都是用于排序的bif(built-in-function)内建函数,区别在于sort()没有返回值,而且sort()内只能传一个list参数,其他可迭代对象不可以,并且它是把list(列表)原地排序,也就是使用后并不是返回一个有序的序列副本,而是把当前序列变得有序,而sorted()参数可以传所有的iterable(可迭代对象),并且,他会通过排序后返回一个新的序列。相比之下,sorted更强大。

list.sort()举例:

>>> a = [9,5,6,8,47,7,5,3,6]

>>> a.sort()

>>> a

[3, 5, 5, 6, 6, 7, 8, 9, 47]

>>> a = 'chen'

>>> a.sort()

Traceback (most recent call last):

File "", line 1, in

a.sort()

AttributeError: 'str' object has no attribute 'sort'

>>>

sorted(iterable)举例:

>>> a = [9,5,6,8,47,7,5,3,6]

>>> sorted(a)

[3, 5, 5, 6, 6, 7, 8, 9, 47]

>>>

可选的参数有三个,cmp、key和reverse。

1)cmp指定一个定制的比较函数,这个函数接收两个参数(iterable的元素),如果第一个参数小于第二个参数,返回一个负数;如果第一个参数等于第二个参数,返回零;如果第一个参数大于第二个参数,返回一个正数。默认值为None。

2)key指定一个接收一个参数的函数,这个函数用于从每个元素中提取一个用于比较的关键字。默认值为None。

3)reverse是一个布尔值。如果设置为True,列表元素将被倒序排列。

通常来说,key和reverse比一个等价的cmp函数处理速度要快。这是因为对于每个列表元素,cmp都会被调用多次,而key和reverse只被调用一次。

cmp参数举例(cmp参数可以和lambda函数一起使用):

>>> L = [('b',2),('a',1),('c',3),('d',4)]

>>> sorted(L, cmp=lambda x,y:cmp(x[1],y[1]))

[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

>>>

key举例:

>>> L = [('b',2),('a',1),('c',3),('d',4)]

>>> sorted(L, key=lambda x:x[1])

[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

>>>

reverse举例:

>>> L = [1,4,3,2,5]

>>> sorted(L)

[1, 2, 3, 4, 5]

>>> sorted(L,reverse=True)

[5, 4, 3, 2, 1]

>>>

enumerate()函数

一般情况下对一个列表或数组既要遍历索引又要遍历元素时,就可以用这个函数。

一般我们要循环列表或数组的索引和元素时,可以这样:

>>> lis = ['chen','ning','love','python']

>>> for i in range(0,len(lis)):

print i,lis[i]

0 chen

1 ning

2 love

3 python

>>>

但是这种方法有些累赘,使用 内置enumerrate函数会有更加直接,优美的做法,先看看enumerate的定义:

def enumerate (collection ):

'Generates an indexed series: (0,coll[0]), (1,coll[1]) ...'

i = 0

it = iter (collection )

while 1 :

yield (i , it . next ())

i += 1

enumerate会将数组或列表组成一个索引序列。使我们再获取索引和索引内容的时候更加方便如下:

>>> lis = ['chen','ning','love','python']

>>> for index,text in enumerate(lis):

print index,text

0 chen

1 ning

2 love

3 python

>>>

在 cookbook里介绍,如果你要计算文件的行数,可以这样写:

count = len (open (thefilepath , ‘ rU ’ ). readlines ())

前面这种方法简单,但是可能比 较慢,当文件比较大时甚至不能工作,下面这种循环读取的方法更合适些。

Count = - 1

For count , line in enumerate (open (thefilepath , ‘ rU ’ )):

Pass

Count += 1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值