Python programming function copies shades +)

Python Functional Programming

This requires proper look at it, after all functional programming in Python also made reference.

python in functional programming support:

The filter function corresponds to the function of the filter. A Boolean function call bool_functo iterate through each element in seq; so returns a bool_seqreturn value of true elements in a sequence.

 

 

>>>a = [1,2,3,4,5,6,7]

>>>b = filter(lambda x: x > 5, a)

>>>print b

>>>[6,7]

function map each item is a sequence of function sequentially executed, the following is a sequence for each item multiplied by 2:

 

 

>>> a = map(lambda x:x*2,[1,2,3])

>>> list(a)

[2, 4, 6]

Iterative reduce function call is a function of each sequence item, following a factorial 3:

 

 

>>> reduce(lambda x,y:x*y,range(1,4))

6

 

python replication, deep and shallow copy copy difference (https://home.cnblogs.com/u/xueli/ from: Li m bloggers )

 

In python, the object assignment is actually a reference to an object. When you create an object and assign it to another variable of time, python and no copy of the object, but only a copy of this object reference

There are three general methods,

alist=[1,2,3,["a","b"]]

 

(1) direct assignment, refers to the default shallow copy of the object passed it, changed the original list, the assigned b will do the same change

>>> b=alist
>>> print b
[1, 2, 3, ['a', 'b']]
>>> alist.append(5)
>>> print alist;print b
[1, 2, 3, ['a', 'b'], 5]
[1, 2, 3, ['a', 'b'], 5]

(2) copy a shallow copy, no copies of the child object, the original data is changed, the child object will change

>>> import copy

>>> c=copy.copy(alist)
>>> print alist;print c
[1, 2, 3, ['a', 'b']]
[1, 2, 3, ['a', 'b']]
>>> alist.append(5)
>>> print alist;print c
[1, 2, 3, ['a', 'b'], 5]
[1, 2, 3, ['a', 'b']]

Alist >>> [. 3]
[ 'A', 'B']
>>> alist [. 3] .append ( 'CCCC')
>>> Print alist; Print C
[. 1, 2,. 3, [ 'A', 'B', 'CCCC'],. 5]
[. 1, 2,. 3, [ 'a', 'B', 'CCCC']] inside the sub-object is changed

 

 

(3) a deep copy, copy contains objects from inside the object, so changing the original object change will not cause a deep copy in any sub-elements

>>> import copy

>>> d=copy.deepcopy(alist)
>>> print alist;print d
[1, 2, 3, ['a', 'b']]
[1, 2, 3, ['a', 'b']]始终没有改变
>>> alist.append(5)
>>> print alist;print d
[1, 2, 3, ['a', 'b'], 5]
[1, 2, 3, ['a', 'b']]始终没有改变
>>> alist[3]
['a', 'b']
>>> alist[3].append("ccccc")
>>> print alist;print d
[1, 2, 3, ['a', 'b', 'ccccc'], 5]
[1, 2, 3, ['a', 'b']]  始终没有改变

 

 /******************************************************************************************************************/

之前见过一道面试题目,还挺有意思,输出测试返回true 还是false

考察点:1,不可变原组 和 可变列表的深拷贝,浅拷贝区别

         

 

 

在python中,对象赋值实际上是对象的引用。当创建一个对象,然后把它赋给另一个变量的时候,python并没有拷贝这个对象,而只是拷贝了这个对象的引用

一般有三种方法,

alist=[1,2,3,["a","b"]]

 

(1)直接赋值,默认浅拷贝传递对象的引用而已,原始列表改变,被赋值的b也会做相同的改变

>>> b=alist
>>> print b
[1, 2, 3, ['a', 'b']]
>>> alist.append(5)
>>> print alist;print b
[1, 2, 3, ['a', 'b'], 5]
[1, 2, 3, ['a', 'b'], 5]

(2)copy浅拷贝,没有拷贝子对象,所以原始数据改变,子对象会改变

>>> import copy

>>> c=copy.copy(alist)
>>> print alist;print c
[1, 2, 3, ['a', 'b']]
[1, 2, 3, ['a', 'b']]
>>> alist.append(5)
>>> print alist;print c
[1, 2, 3, ['a', 'b'], 5]
[1, 2, 3, ['a', 'b']]

>>> alist[3]
['a', 'b']
>>> alist[3].append('cccc')
>>> print alist;print c
[1, 2, 3, ['a', 'b', 'cccc'], 5]
[1, 2, 3, ['a', 'b', 'cccc']] 里面的子对象被改变了

 

 

(3)深拷贝,包含对象里面的自对象的拷贝,所以原始对象的改变不会造成深拷贝里任何子元素的改变

>>> import copy

>>> d=copy.deepcopy(alist)
>>> print alist;print d
[1, 2, 3, ['a', 'b']]
[1, 2, 3, ['a', 'b']]始终没有改变
>>> alist.append(5)
>>> print alist;print d
[1, 2, 3, ['a', 'b'], 5]
[1, 2, 3, ['a', 'b']]始终没有改变
>>> alist[3]
['a', 'b']
>>> alist[3].append("ccccc")
>>> print alist;print d
[1, 2, 3, ['a', 'b', 'ccccc'], 5]
[1, 2, 3, ['a', 'b']]  始终没有改变

 

 /******************************************************************************************************************/

之前见过一道面试题目,还挺有意思,输出测试返回true 还是false

考察点:1,不可变原组 和 可变列表的深拷贝,浅拷贝区别

         

 

Guess you like

Origin www.cnblogs.com/xqy-yz/p/11370659.html