Skip to content

Commit c8ec754

Browse files
author
kangxiaoyu
committed
切片,迭代,列表生成式,生成器的代码练习
1 parent 46e1c84 commit c8ec754

23 files changed

+236
-0
lines changed

2017-04-24/diedaiqi/odd_g.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
#这里,最难理解的就是generator和函数的执行流程不一样。
4+
#函数是顺序执行,遇到return语句或者最后一行函数语句就返回。i
5+
#而变成generator的函数,在每次调用next()的时候执行,遇到yield语句返回,再次执行时从上次返回的yield语句处继续执行。
6+
def odd():
7+
print 'step 1'
8+
yield 1
9+
print 'step 2'
10+
yield 3
11+
print 'step 3'
12+
yield 5
13+
o = odd()
14+
print o.next()
15+
print o.next()
16+
print o.next()

2017-04-24/diedaiqi/triangle.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
#我爱中文
4+
def fib(max):
5+
n,a,b=0,0,1
6+
while n < max:
7+
yield b
8+
a,b=b,a+b
9+
n=n+1
10+
print fib(6)

2017-04-24/filter/filter_empty.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
def not_empty(s):
4+
return s and s.strip()
5+
print filter(not_empty,['A','','B',None,'C',''])

2017-04-24/filter/filter_odd.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
def is_odd(n):
4+
return n%2 == 1
5+
6+
print filter(is_odd,[1,2,4,5,6,9,10,15])

2017-04-24/generator/generator2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
L=[x*x for x in range(10)]
4+
print L
5+
g=(x*x for x in range(10))
6+
print g
7+
#建L和g的区别仅在于最外层的[]和(),L是一个list,而g是一个generator。
8+
9+
#我们可以直接打印出list的每一个元素,但我们怎么打印出generator的每一个元素呢?
10+
11+
#如果要一个一个打印出来,可以通过next()函数获得generator的下一个返回值:
12+
for n in g:
13+
print(n)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
L=[x*x for x in range(10)]
4+
print L
5+
g=(x*x for x in range(10))
6+
print g
7+
#建L和g的区别仅在于最外层的[]和(),L是一个list,而g是一个generator。
8+
9+
#我们可以直接打印出list的每一个元素,但我们怎么打印出generator的每一个元素呢?
10+
11+
#如果要一个一个打印出来,可以通过next()函数获得generator的下一个返回值:
12+
13+
next(g)
14+
next(g)
15+
next(g)
16+
next(g)
17+
next(g)
18+
next(g)
19+
next(g)
20+
next(g)
21+
next(g)
22+
next(g)
23+
next(g)

2017-04-24/high_order/high1.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/env python
2+
#-*- coding:utf-8 -*-
3+
#那么函数名是什么呢?函数名其实就是指向函数的变量!对于abs()这个函数,完全可以把函数名abs看成变量,它指向一个可以计算绝对值的函数!
4+
5+
#如果把abs指向其他对象,会有什么情况发生?
6+
7+
abs = 10
8+
print abs(-10)

2017-04-24/high_order/map_first.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
def f(x):
4+
return x * x
5+
print map(f,[1,2,3,4,5,6,7,8,9])
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
def add(x,y,f):
4+
return f(x) + f(y)
5+
print add(-5,6,abs)
6+
#把函数作为参数传入,这样的函数称为高阶函数,函数式编程就是指这种高度抽象的编程范式

2017-04-24/map_fun/map_first.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
def f(x):
4+
return x * x
5+
print map(f,[1,2,3,4,5,6,7,8,9])

2017-04-24/map_fun/map_str.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
#这儿可以写中文
4+
print map(str,[1,2,3,4,5,6,7,8,9])

2017-04-24/map_fun/nap_reduce_char.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
def fn(x,y):
4+
return x * 10 + y
5+
def char2num(s):
6+
return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[s]
7+
print reduce(fn, map(char2num,'13579'))

2017-04-24/map_fun/reduce_add.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
def add(x,y):
4+
return x + y
5+
print reduce(add,[1,3,5,7,9])

2017-04-24/map_fun/reduce_map.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#!/bin/env pytho

2017-04-24/map_fun/ten_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#! /bin/env python
2+
# -*- coding:utf-8 -*-
3+
def add(x, y):
4+
return x*10 + y
5+
6+
print reduce(add,[1,3,5,7,9])

2017-04-24/return_func/closure_fun.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
def count():
4+
fs=[]
5+
for i in range(1,4):
6+
def f(j):
7+
def g():
8+
return j*j
9+
return g
10+
fs.append(f(i))
11+
return fs
12+
f1,f2,f3=count()
13+
print f1()
14+
print f2()
15+
print f3()
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+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
def calc_sum(*args):
4+
ax=0
5+
for n in args:
6+
ax=ax+n
7+
return ax
8+
def lazy_sum(*args):
9+
def sum():
10+
ax=0
11+
for n in args:
12+
ax=ax+n
13+
return ax
14+
return sum
15+
f1=lazy_sum(1,3,5,7,9)
16+
f2=lazy_sum(1,3,5,7,9)
17+
print f1==f2
18+
#print f
19+
#print f()

2017-04-24/sorted/ignoreStrSorted.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/env python
2+
def cmp_ignore_case(s1,s2):
3+
u1 = s1.upper()
4+
u2 = s2.upper()
5+
if u1 < u2:
6+
return 1
7+
if u1 > u2:
8+
return 1
9+
return 0
10+
print sorted(['bob','about','Zoo','Credit'],cmp_ignore_case)

2017-04-24/sorted/reversed_cmp.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
def reversed_cmp(x,y):
4+
if x>y:
5+
return -1
6+
if x < y:
7+
return 1
8+
return 0
9+
print sorted([36,5,12,9,21],reversed_cmp)

2017-04-24/sorted/sort.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
print sorted([36,5,12,9,21])

2017-04-24/sorted/strSorted.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/env python
2+
print sorted(['bob','about','Zoo','Credit'])

2017-04-24p/f_lambda.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
f=lambda x:x*x
4+
print f
5+
print f(2)

2017-04-24p/map_lambda.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
print map(lambda x:x*x,[1,2,3,4,5,6,7,8,9])

0 commit comments

Comments
 (0)