Skip to content

Commit 955f01c

Browse files
committed
增加了使用python中filter函数的方法
1 parent 46ac7e9 commit 955f01c

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

even_odd.md

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818

1919
##本问题由黄老师提供并解决,[他的微博](http://weibo.com/qiyeminglu?from=feed&loc=nickname)
2020

21+
#解决(python)
22+
23+
def odd(x):return x%2==1 #判断是否为奇数,是则返回true
24+
def even(x):return x%2==0
25+
26+
if __name__=="__main__":
27+
test_lst = [7,9,12,5,4,9,8,3,12,89]
28+
print filter(even,test_lst)+filter(odd,test_lst) #利用filter函数
29+
2130
#解决 (racket 5.2.1)
2231

2332
```racket

even_odd.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
21
#! /usr/bin/env python
32
#coding:utf-8
43

4+
'''
5+
#way1
56
67
def odd_even_sort(lst):
78
@@ -10,7 +11,19 @@ def odd_even_sort(lst):
1011
tmp_list2 = [item for item in lst if not is_odd_number(item)]
1112
return tmp_list1+tmp_list2
1213
14+
1315
if __name__=="__main__":
1416
test_lst = [7,9,12,5,4,9,8,3,12,89]
1517
1618
print odd_even_sort(test_lst)
19+
20+
'''
21+
22+
#way2
23+
24+
def odd(x):return x%2==1
25+
def even(x):return x%2==0
26+
27+
if __name__=="__main__":
28+
test_lst = [7,9,12,5,4,9,8,3,12,89]
29+
print filter(even,test_lst)+filter(odd,test_lst)

0 commit comments

Comments
 (0)