python基础回顾【二】

2019-01-05  本文已影响0人  一个热爱生活的好少年

11:迭代器:

#最简单的迭代
>>> d = {'a': 1, 'b': 2, 'c': 3}
>>> for key in d:
... print(key)
#...
#a
#c
#b
#默认情况下,dict 迭代的是 key。如果要迭代 value,可以用 for value in
#d.values(),如果要同时迭代 key 和 value,可以用 for k, v in d.items()。
 list1 =[1, 2, 3]
 it = iter(list1)
 print( next(it))
 print( next(it))
 print( next(it))
 print( next(it)) # except报错

#yield:如果没有   while会把所有的放回,这个可以记录当前的输出位置并且下次继续从输出位置开始,自构迭代器。
# # def frange(start, stop, step):
# #     x = start
# #     while x < stop:
# #         yield x
# #         x += step
# #
# #
# # for i in frange(10,20,0.5):
# #     print(i)

12:lamdba表达式:

 def true():return True
 lambda : True

#如何写:
# # >>> def add(x,y):
# # ...     return x+y

>>> def add(x,y): return x+y
    lambda  x,y: x+y

# # lambda  x:x<= (month, day)
# #
# # def func1(x):
# #     return  x<= (month, day)

13:内置函数:

#filter(判断函数,序列)
def is_odd(n):
    return n % 2 == 1 
newlist = filter(is_odd, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

 a=[1,2,3,4,5,6,7]
 list(filter(lambda x:x>2 , a))

#map/reduce
#map()函数接收两个参数,一个是函数,一个是 Iterable
>>> def f(x):
... return x * x
...
>>> r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> list(r)
[1, 4, 9, 16, 25, 36, 49, 64, 81]

 >>> a=[1,2,3]
 >>> map(lambda x:x ,a)
 <map object at 0x105686e10>
 >>> list(map(lambda x:x ,a))
 [1, 2, 3]
 >>> list(map(lambda x:x+1 ,a))
 [2, 3, 4]
#多序列计算
 >>> b=[4,5,6]
 >>> list(map(lambda x,y:x+y ,a,b))
 [5, 7, 9]

#reduce 把一个函数作用在一个序列[x1, x2, x3, ...]
#上,这个函数必须接收两个参数,reduce 把结果继续和序列的下一个元
#素做累积计算,其效果就是:
#reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

# >>> from functools import reduce
# >>> reduce(lambda x,y: x+y ,[2,3,4],1 )
# 10
# >>> ((1+2)+3)+4

>>> from functools import reduce
>>> def fn(x, y):
... return x * 10 + y
...
>>> reduce(fn, [1, 3, 5, 7, 9])
13579

#
# >>> for i in zip((1,2,3),(4,5,6)):
# ...     print(i)
# ...
# (1, 4)
# (2, 5)
# (3, 6)

#zip()

14:
闭包:

#外部函数a被内部函数引用就是闭包
def sum(a):
    def add(b):
      return  a+b

    return add

# add 函数名称或函数的引用
# add() 函数的调用
#输入a
num2 =  sum(2)
#输入b
print( num2(4))

例子:
def counter(FIRST=0):
    cnt = [FIRST]
    #cnt这个值 会存储要加的数:调用的次数越多,数可以一直加

    def add_one(FIRST_1=1):
        cnt[0] += FIRST_1
        return cnt[0]

    return add_one


num5 = counter(5)
num10 = counter(10)
#可以分布传值
print(num5())
print(num5())

15:装饰器

import time

# print( time.time())

def timmer(func):
    def wrapper():
        start_time = time.time()
#要执行的函数
        func()
        stop_time = time.time()
        print("运行时间是 %s 秒 " % (stop_time - start_time))
    return wrapper


#传入的是函数
@timmer
def i_can_sleep():
    time.sleep(3)

例子:带参数
def new_tips(argv):
    def tips(func):
        def nei(a, b):
            print('start %s %s' % (argv, func.__name__))
            func(a, b)
            print('stop')

        return nei
    return tips


@new_tips('add_module')
def add(a, b):
    print(a + b)


@new_tips('sub_module')
def sub(a, b):
    print(a - b)


print(add(4, 5))
print(sub(7, 3))

16:模块


image.png
上一篇 下一篇

猜你喜欢

热点阅读