Skip to content

Commit 909b4a5

Browse files
author
xy
committed
nothin
1 parent 6ab6ad1 commit 909b4a5

File tree

12 files changed

+80
-191
lines changed

12 files changed

+80
-191
lines changed

chapter11/filter.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
__author__ = 'xy'
4+
5+
def filter_(bool_func, seq):
6+
filtered_seq = []
7+
for eachItem in seq:
8+
if bool_func(eachItem):
9+
filtered_seq.append(eachItem)
10+
return filtered_seq
11+
12+
13+
from random import randint
14+
15+
16+
def odd(n):
17+
return n % 2
18+
19+
20+
allNums = []
21+
for eachNum in range(9):
22+
allNums.append(randint(1, 99))
23+
24+
print filter(odd, allNums)
25+
26+
# 第一次改进: print filter(lambda x: x % 2, allNums)
27+
# 第二次改进:用list-if代替filter print [n for n in allNums if n%2]
28+
# 第三次改进: print [n for n in [randint(1, 99) for i in range(9)] if n%2]

chapter11/global.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
__author__ = 'xy'
4+
5+
6+
is_this_global = 'xyz'
7+
def foo():
8+
global is_this_global # 在局部使用全局变量之前先声明gloabl
9+
this_is_global = 'abc'
10+
is_this_global = 'def'
11+
print this_is_global + is_this_global
12+
13+
foo()

chapter11/lambda.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
__author__ = 'xy'
4+
5+
def add(x, y):
6+
return x + y
7+
8+
sum = lambda x, y: x + y

chapter11/map.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
__author__ = 'xy'
4+
5+
6+
def map_(func, seq):
7+
mapped_seq = []
8+
for eachItem in seq:
9+
mapped_seq.append(func(eachItem))
10+
return mapped_seq
11+
12+
13+
print map(lambda x: x**2, range(6))
14+
print map(lambda x, y: x+y, [1, 2], [3, 4]) # 用两个list输入两个参数
15+
print map(None, [1, 3, 5], [2, 4, 6]) # 功能与zip()相同

chapter11/reduce.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
__author__ = 'xy'
4+
5+
def reduce_(bin_func, seq, init=None):
6+
Iseq = list(seq)
7+
if init is None:
8+
res = Iseq.pop()
9+
else:
10+
res = init
11+
for item in Iseq:
12+
res = bin_func(res, item)
13+
return res
14+
15+
16+
print 'the total is :', reduce((lambda x, y: x+y), range(5))

chapter9/practice/9_1.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

chapter9/practice/9_1.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

chapter9/practice/9_4.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

chapter9/practice/9_6.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

chapter9/practice/a_.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)