码迷,mamicode.com
首页 > 其他好文 > 详细

内置函数,递归函数

时间:2017-06-30 13:47:54      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:from   error:   not   int   不同   play   most   enumerate   alt   

divmod函数

技术分享
divmod(7,3)
(2, 1)
>>> a,b=divmod(7,3)
>>> a
2
>>> b
1
>>> help(divmod)
View Code

divmod返回一个tuple ,(x//y,x%y)

enumerate(iterable)

返回的事一个迭代器,包含他的值和索引

技术分享
for i in enumerate(l):
print(i)


(0, ‘a‘)
(1, ‘b‘)
(2, ‘c‘)
View Code
技术分享
for i in enumerate(dic):
    print(i)

    
(0, ‘name‘)
(1, ‘age‘)
View Code

frozenset()创建一个被冰冻的集合,不可删除,不可添加

技术分享
s=frozenset((1,2,3,4))
>>> s.pop()
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    s.pop()
AttributeError: ‘frozenset‘ object has no attribute ‘pop‘
>>> s.add(1)
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    s.add(1)
AttributeError: ‘frozenset‘ object has no attribute ‘add‘
View Code

globals()  locals()  返回字典形式的,当前范围内的全局变量/局部变量

技术分享
def fun():
    x=1
    print(locals())

    
>>> fun()
{‘x‘: 1}
>>> def fun():
    x=1
    print(globals())

    
>>> fun()
{‘__name__‘: ‘__main__‘, ‘__doc__‘: None, ‘__package__‘: None, ‘__loader__‘: <class ‘_frozen_importlib.BuiltinImporter‘>, ‘__spec__‘: None, ‘__annotations__‘: {}, ‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘a‘: 2, ‘b‘: 1, ‘l‘: [‘a‘, ‘b‘, ‘c‘], ‘i‘: (1, ‘age‘), ‘dic‘: {‘name‘: ‘egon‘, ‘age‘: 18}, ‘s‘: frozenset({1, 2, 3, 4}), ‘fun‘: <function fun at 0x0000000003600950>}
View Code

isinstance() 判断类型

type(s)
<class ‘frozenset‘>
>>> isinstance(s,frozenset)
True

zip()  返回一个元组,参数为两个可迭代的对象。

技术分享
l1=[1,2,3,4]
>>> l2=[‘a‘,‘b‘,‘c‘,‘d‘]
>>> l=zip(l1,l2)
>>> type(l)
<class ‘zip‘>
>>> for i in l:
    print(i)

    
(1, ‘a‘)
(2, ‘b‘)
(3, ‘c‘)
(4, ‘d‘)
View Code

max() min()   返回最大值,最小值,参数可以是一个可迭代对象或者多个普通参数

匿名函数:

格式:lambda 参数,参数: 函数表达式

应用于一些很简单的函数,自带return 效果

map() ,映射函数

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

比如将一个列表的每个元素都平方得到一个新的序列,可以用map方法

技术分享
 def f(x):
    return x*x
View Code
技术分享
>>> list(map(f,l))
[1, 4, 9, 16, 25]
View Code

reduce()函数

from functools import reduce

使用reduce,必须要导入from functools import reduce     reduce把一个函数作用在一个序列[x1, x2, x3, ...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,其效果就是:

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

技术分享
from functools import reduce
r=reduce(lambda x,y:x*y,[1,2,3,4,5])
print(r)
View Code
技术分享
from functools import reduce
# r=reduce(lambda x,y:x*y,[1,2,3,4,5])
# print(r)

def char2num(s):
    return {‘0‘: 0, ‘1‘: 1, ‘2‘: 2, ‘3‘: 3, ‘4‘: 4, ‘5‘: 5, ‘6‘: 6, ‘7‘: 7, ‘8‘: 8, ‘9‘: 9}[s]

r=map(char2num,‘13567‘)
# for i in r:
#     print(i)
r=list(r)
print(r)
n=reduce(lambda x,y:x*10+y,r)
print(n)
View Code

filter函数
和map类似,filter()也接收一个函数和一个序列。和map()不同的是,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。
技术分享
r=filter(lambda x:x%2 == 1,[1,2,34,5,7])
r=list(r)
print(r)
View Code

 




 

内置函数,递归函数

标签:from   error:   not   int   不同   play   most   enumerate   alt   

原文地址:http://www.cnblogs.com/yuyang26/p/7055209.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!