Python这7个好用内置函数!

这篇文章咱们来看几个颇有用的 Python 内置函数 ,我认为每一个学习 Python的 都应该知道这些函数。python

对于每一个函数,我会使用一个普通的实现来和内置函数作对比。数组

若是我直接引用了内置函数的文档,请理解,由于这些函数文档写的很是棒!app

all(iterable)ide

若是可迭代的对象(数组,字符串,列表等,下同)中的元素都是 true (或者为空)的话返回 True 。函数

_all = True
for item in iterable:
    if not item:
        _all = False
        break
if _all:
    # do stuff

更简便的写法是:学习

if all(iterable):
    # do stuff

any(iterable)优化

若是可迭代的对象中任何一个元素为 true 的话返回 True 。若是可迭代的对象为空则返回 False 。ui

'''
遇到问题没人解答?小编建立了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
_any = False
for item in iterable:
    if item:
        _any = True
        break
if _any:
    # do stuff

更简便的写法是:code

if any(iterable):
    # do stuff

cmp(x, y)视频

比较两个对象 x 和 y 。x < y 的时候返回负数, x ==y 的时候返回 0, x > y 的时候返回正数。

def compare(x,y):
    if x < y:
        return -1
    elif x == y:
        return 0
    else:
        return 1

你彻底可使用一句 cmp(x, y) 来替代。

dict([arg])

使用 arg 提供的条目生成一个新的字典。

arg 一般是未知的,可是它很方便!好比说,若是咱们想把一个含两个元组的列表转换成一个字典,咱们能够这么作。

'''
遇到问题没人解答?小编建立了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
l = [('Knights', 'Ni'), ('Monty', 'Python'), ('SPAM', 'SPAAAM')]
d = dict()
for tuple in l:
    d[tuple[0]] = tuple[1]
# {'Knights': 'Ni', 'Monty': 'Python', 'SPAM': 'SPAAAM'}

或者这样:

l = [('Knights', 'Ni'), ('Monty', 'Python'), ('SPAM', 'SPAAAM')]
d = dict(l) # {'Knights': 'Ni', 'Monty': 'Python', 'SPAM': 'SPAAAM'}

enumerate(iterable [,start=0])

我真的是超级喜欢这个!若是你之前写过 C 语言,那么你可能会这么写:

for i in range(len(list)):
    # do stuff with list[i], for example, print it
    print i, list[i]

噢,不用那么麻烦!你可使用 enumerate() 来提升可读性。

for i, item in enumerate(list):
    # so stuff with item, for example print it
    print i, item

isinstance(object, classinfo)

若是 object 参数是 classinfo 参数的一个实例或者子类(直接或者间接)的话返回 True 。

当你想检验一个对象的类型的时候,第一个想到的应该是使用 type() 函数。

if type(obj) == type(dict):
    # do stuff
elif type(obj) == type(list):
    # do other stuff
...

或者你能够这么写:

if isinstance(obj, dict):
    # do stuff
elif isinstance(obj, list):
    # do other stuff
...

pow(x, y [,z])

返回 x 的 y 次幂(若是 z 存在的话则以 z 为模)。

若是你想计算 x 的 y 次方,以 z 为模,那么你能够这么写:

mod = (x ** y) % z

可是当 x=1234567, y=4567676, z=56 的时候个人电脑足足跑了 64 秒!

不要用 ** 和 % 了,使用 pow(x, y, z) 吧!这个例子能够写成 pow(1234567, 4567676, 56) ,只用了 0.034 秒就出告终果!

zip([iterable, ])

这个函数返回一个含元组的列表,具体请看例子。

'''
遇到问题没人解答?小编建立了一个Python学习交流QQ群:××× 寻找有志同道合的小伙伴,
互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
l1 = ('You gotta', 'the')
l2 = ('love', 'built-in')
out = []
if len(l1) == len(l2):
    for i in range(len(l1)):
        out.append((l1[i], l2[i]))
# out = [('You gotta', 'love'), ('the', 'built-in)]

或者这么写:

l1 = ['You gotta', 'the']
l2 = ['love', 'built-in']
out = zip(l1, l2) # [('You gotta', 'love'), ('the', 'built-in)]

若是你想获得倒序的话加上 * 操做符就能够了。

print zip(*out)
# [('You gotta', 'the'), ('love', 'built-in')]

结论

Python 内置函数很方便,它们很快而且通过了优化,因此它们可能效率更高。

我真心认为每一个 Python 开发者都应该好好看看内置函数的文档(引言部分)。

忘了说了,在 itertools 模块中有不少很不错的函数。

相关文章
相关标签/搜索