Skip to content

Commit 0855e8a

Browse files
committed
map reduce
1 parent 4b9984a commit 0855e8a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

studyLib/functional/map_reduce.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
from functools import reduce
5+
import re
56

67
__author__ = 'Mr.Huo'
78

@@ -22,6 +23,10 @@ def statistics(dic, k):
2223
return dic
2324

2425

26+
def str2int(x, y):
27+
return x * 10 + y
28+
29+
2530
def main():
2631
# python map()函数,接收两个参数函数、Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回
2732
list1 = [x for x in range(1, 12)]
@@ -48,6 +53,20 @@ def main():
4853
redu4 = reduce(statistics, list2, {})
4954
print(redu4)
5055

56+
# 将字符串的首字母大写,其他的小写
57+
names = ['adam', 'LISA', 'barT']
58+
b = lambda x: x[0].upper() + x[1:].lower()
59+
print(b(names[2]))
60+
new_names = list(map(lambda x: x[0].upper() + x[1:].lower(), names))
61+
print(new_names)
62+
63+
# 将数字字符串转换成一个数
64+
num_str = '1003750'
65+
num = reduce(lambda x, y: x * 10 + y, map(int, num_str))
66+
print(num, type(num))
67+
num1 = reduce(lambda x, y: x / 10 + y, map(int, num_str[::-1]))
68+
print(num1, type(num1))
69+
5170

5271
if __name__ == '__main__':
5372
main()

0 commit comments

Comments
 (0)