forked from xianhu/LearnPython
-
Notifications
You must be signed in to change notification settings - Fork 410
/
python_functional.py
74 lines (59 loc) · 1.67 KB
/
python_functional.py
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
# _*_ coding: utf-8 _*_
from fn import _
from operator import add
from functools import partial, reduce
# 列表解析
a_list = [item**2 for item in range(5)]
print(a_list)
# 字典解析
a_dict = {"%d^2" % item: item**2 for item in range(5)}
print(a_dict)
# 生成器
a_generator = (item**2 for item in range(5))
print(a_generator)
print(next(a_generator))
print(next(a_generator))
# iter函数和next函数
a_list_generator = iter(a_list)
print(next(a_list_generator))
print(next(a_list_generator))
print(type(a_list), type(a_list_generator))
# lambda表达式
a_func = lambda x, y: x**y
print(a_func(2, 3))
# map函数
print(map(abs, range(-4, 5)))
print(list(map(abs, range(-4, 5))))
print(list(map(lambda x: x**2, range(5))))
print(list(map(lambda x, y: x**y, range(1, 5), range(1, 5))))
# reduce函数
print(reduce(lambda x, y: x+y, range(10)))
print(reduce(lambda x, y: x+y, range(10), 100))
print(reduce(lambda x, y: x+y, [[1, 2], [3, 4]], [0]))
# filter函数
print(filter(None, range(-4, 5)))
print(list(filter(None, range(-4, 5))))
print(list(filter(lambda x: x > 0, range(-4, 5))))
# all、any函数
print(all([0, 1, 2]))
print(any([0, 1, 2]))
# enumerate函数
for index, item in enumerate(range(5)):
print("%d: %d" % (index, item))
# zip函数
for a, b in zip([1, 2, 3], ["a", "b", "c"]):
print(a, b)
a_dict = dict(zip([1, 2, 3], ["a", "b", "c"]))
print(a_dict)
# partial函数
print(int("10010", base=2))
int_base_2 = partial(int, base=2)
print(int_base_2("10010"))
# operator.add函数
print(reduce(lambda x, y: x+y, range(10)))
print(reduce(add, range(10)))
# fn的使用
add_func_1 = (_ + 2)
print(add_func_1(1))
add_func_2 = (_ + _ * _)
print(add_func_2(1, 2, 3))