-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzhbing001.py
More file actions
194 lines (158 loc) · 3.39 KB
/
Copy pathzhbing001.py
File metadata and controls
194 lines (158 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- coding:utf-8 -*-
# __author__ = 'zhbing'
# l=[x*x for x in range(10)]
# print(l)
# 创建generator
# l0=(x*x for x in range(10))
# print(l0)
# print(next(l0))
# print(next(l0))
# for i in l0:
# print(i,end='\t')
# def fib1(max):
# n, a, b = 1, 1, 1
# while n <= max:
# print(a)
# a, b = b, a + b
# n = n + 1
# fib1(5)
#
# def fib2(max):
# n, a, b = 1, 1, 1
# while n <= max:
# yield a
# a, b = b, a + b
# n = n + 1
# print(fib2(3))
#######################################################
# generator生成器实现杨辉三角
# def triangle_yang():
# L=[1]
# while True:
# yield L
# L=[L[x]+L[x+1] for x in range(len(L)-1)]
# L=[1]+L+[1]
#
# n=0
# for i in triangle_yang():
# print(i)
# n=n+1
# if n==10:
# break
####################################################
# from collections import Iterable,Iterator
# print(isinstance([],Iterable))
# print(isinstance(100,Iterable))
#
# print(isinstance((),Iterator))
# print(isinstance(iter(()),Iterator))
# print(isinstance((x*x for x in range(10)),Iterator))
# def f00(x,y,f):
# print(f(x)+f(y))
#
# f00(3,-6,abs)
# def f01(x):
# return x*x
#
# v=map(f01,[1,2,3,4,5,6])
# print(list(v))
from functools import reduce
# def f02(x,y):
# return 10*x+y
# print(reduce(f02,[1,3,5,7,9]))
# def str2int(s):
# def f02(x,y):
# return 10*x+y
#
# def charm2num(s):
# return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[s]
#
# return reduce(f02,map(charm2num,s))
#
# print(str2int('111222333'))
# def is_odd(n):
# return n%2==1
#
# l=list(filter(is_odd,[1,2,3,4,5,6,7,8,9]))
# print(l)
# l=[21,-10,35,-6,-77,9]
# print(sorted(l,key=abs))
#
# l0=['apple','Microsoft','google','IBM','Nokia']
# print(sorted(l0,key=str.lower,reverse=True))
# def lazy_sum(*args):
# def sum():
# ax=0
# for n in args:
# ax+=n
# return ax
# return sum
#
# f=lazy_sum(1,2,3,4,5)
# print(f)
# print(f())
# def count():
# fs=[]
# for i in range(1,4):
# def f():
# return i*i
# fs.append(f)
# return fs
#
# f1,f2,f3=count()
# print(f1(),f2(),f3())
# 返回函数不要引用任何循环变量,或者后续会发生变化的变量
# def count():
# def f(j):
# def g():
# return j*j
# return g
# fs=[]
# for i in range(1,4):
# fs.append(f(i)) #f(i)立刻被执行,因此i的当前值被传入f()
# return fs
#
# f1,f2,f3=count()
# print(f1(),f2(),f3())
# print(list(map(lambda x:x*x,[1,2,3,4,5,6,7,8,9])))
#
# def build(x,y):
# return lambda:x*x+y*y
#
# print(build(3,4)())
#装饰器
# def log(func):
# def wrapper(*args,**kw):
# print('call %s函数:' % func.__name__)
# return func(*args,*kw)
# return wrapper
#
# @log
# def now():
# print('2017-06-13')
#
# now()
# def log(text):
# def decorator(func):
# def wrapper(*args,**kw):
# print('%s %s():' % (text,func.__name__))
# return func(*args,**kw)
# return wrapper
# return decorator
#
# @log('执行')
# def now():
# print('20170613')
#
# now()
#偏函数
# print(int('15',base=8))
#
# def int2(x,base=2):
# return int(x,base)
#
# print(int2('110'))
# import functools
# int2=functools.partial(int,base=2)
# print(int2('1111'))
# print(int2('1111',base=10))