Have time system read the Python study manual and record some easy-to-forget points
1.python function High Polymerization low coupling
1) Use parameters for input and return for output
2) Use global variables only when it is really necessary
3) do not change the parameters of the mutable type unless the caller wishes to do so
4) Each function should have a single, unified goal
5) Each function should be relatively small
6) Avoid changing variables directly in another file
2. Recursive processing of arbitrary structures
>>> a=[3,[2,[2,3,4],2],1,[1,5,[1,3,3]]
>>> def sumtree (L):
... tot=0
... for x in L:
... if not isinstance (x,list):
... tot+=x
.. else:
... tot+=sumtree (x)
.. return tot
...
>>> Sumtree (a)
30
>>>
3.lamdba,map,filter,reduce usage
>>> map ((lambda x:x+3), [1,2,3,4,5,6])
[4, 5, 6, 7, 8, 9]
>>> map ((lambda x,y:x+y), [1,2,3,4,5,6],[2,3,4,5,6,7])
[3, 5, 7, 9, 11, 13]
>>>
>>> Filter ((lambda x:x%2==0), [1,2,3,4,5,6])
[2, 4, 6]
>>> Reduce ((lambda x,y:x+y), [1,2,3,4,5,6])
21st
>>> Reduce ((lambda x,y:x*y), [1,2,3,4,5,6])
720
>>>
>>> Filter ((lambda x:x%2==0), [1,2,3,4,5,6])
[2, 4, 6]
>>> Reduce ((lambda x,y:x+y), [1,2,3,4,5,6])
21st
>>> Reduce ((lambda x,y:x*y), [1,2,3,4,5,6])
720
>>>
Some easy-to-forget points in the Python Learning Manual (4-7 parts)