编程方法论与三个重要函数

Posted lzjdsg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了编程方法论与三个重要函数相关的知识,希望对你有一定的参考价值。

1.方法论:面向对象编程

     面向过程编程:将一个大步骤分解为许多个小步骤,一步一步解决

     函数式编程:数学式与编程式,强调简洁性,一步解决 

 1 #面向对象:
 2 def test(x):
 3     a = x+1
 4     b = a**2
 5     return b
 6 print(test(10))
 7 #函数式编程:
 8 def test(x):
 9     return (x+1)**2
10 print(test(10))

2.map()函数:对输入迭代对象逐一进行操作在生成迭代器 

 1 list_1 = [1,23,134,111]
 2 def test_1(x):
 3     x**=2
 4     return x
 5 def test(func,array):
 6     new_list=[]
 7     for item in array:
 8         a = func(item)
 9         new_list.append(a)
10     return new_list
11 print(test(test_1,list_1))
12 print(test(lambda x:x**2,list_1))
13 (print(list(map(lambda x:x**2,list_1))) #三个打印结果一样
 1 list_2 = [a,b,c,d]
 2 def test_2(x):
 3     x = x+_sb
 4     return x
 5 def test(func,array):
 6     new_list = []
 7     for item in array:
 8         new_list.append(func(item))
 9     return new_list
10 print(test(test_2,list_2))
11 print(test(lambda x:x+_sb,list_2))
12print(list(map(lambda x:x+_sb,list_2)))

3.filter()函数:通过布尔值的判断来确定过滤出的可迭代对象中的元素(True时打印出)

1 list_3 = [Mr.Li,Mrs.Wang,Mr.Liu,Mrs.Zhang]
2 filter(lambda x:not x.startswith(Mrs),list_3) #<filter object at 0x00000000026B63C8> 表示为迭代器,需要用列表来表示出来
3 print(filter(lambda x:not x.startswith(Mrs),list_3)) #无打印结果
4 print(list(filter(lambda x:not x.startswith(Mrs),list_3)))#正常结果

4.reduce()函数:对所输入可迭代对象进行合并

1 from functools import reduce #需要从模块中导入函数
2 list_4 = [12,11,10]
3 print(reduce(lambda x,y:x+y,list_4))
4 print(reduce(lambda x,y:x+y,list_4,100)) #第三个参数可以自己设定,为加入计算的初始值

 

以上是关于编程方法论与三个重要函数的主要内容,如果未能解决你的问题,请参考以下文章

201555332盛照宗—网络对抗实验1—逆向与bof基础

React 面向组件编程(下)

论Python代码风格与编程习惯的重要性

前端必学——函数式编程

编程方法论

Objective-C Runtime 文档翻译—与Runtime的相互作用