Examples of recursive function calls and anonymous function lambda's summation of 1-100 and calculation factorial examples in Python

1. Script example of recursively listing files in directory
To list the files in the directory, you can do the following: os.listdir()

In [1]: import os

In [4]: os.listdir('/root')

Out[4]:

['.tcshrc',

'.bash_history',

'.bashrc',

'ENV',

'.cache',

'.config',

'.cshrc',

'.bash_logout',

'python',

'.ssh',

'shell',

'.bash_profile',

'.ipython',

'.viminfo',

'dictionary.txt',

'1.txt']

Determine whether it is a directory:

In [5]: os.path.isdir('/home')

Out[5]: True

Determine whether it is a document:

In [7]: os.path.isfile('/etc/rc.local')

Out[7]: True

Absolute path for splicing file name:

In [8]: os.path.join('/etc/','passwd','abc')

Out[8]: '/etc/passwd/abc'

List all file scripts in the directory if:

#!/usr/bin/env python

#FengXiaoqing

# listDir.py

import os

import sys

def print_files(path):

    lsdir = os.listdir(path)

    dirs = [i for i in lsdir if os.path.isdir(os.path.join(path,i))]

    files = [i for i in lsdir if os.path.isfile(os.path.join(path,i))]

    if dirs:

        for d in dirs:

            print_files(os.path.join(path,d))

    if files:

        for f in files:

            print (os.path.join(path,f))

print_files(sys.argv[1])
[root@localhost ~]# tree /tmp
/tmp
├── 123.tx
├── 123.txx
└── a
    └── b
        ├── b.txt
        └── c
            ├── c.txt
            └── d
                └── d.txt

4 directories, 5 files
[root@localhost ~]# 

[root@localhost ~]# python listDir.py /tmp
/tmp/123.tx
/tmp/123.txx
/tmp/a/b/b.txt
/tmp/a/b/c/c.txt
/tmp/a/b/c/d/d.txt
[root@localhost ~]# 

2. Anonymous function lambda

lambda function is a kind of minimum function which can be used to define radio function quickly.

3 * 5 implementation method:

In [1]: def fun(x,y):

...:     return x * y

...:

In [2]: fun(3,5)

Out[2]: 15

Anonymous functions are defined as follows:

In [3]: lambda x,y:x * y

Out[3]: <function __main__.<lambda>>    #Objects returned

In [4]: r = lambda x,y:x * y

In [6]: r(3,5)

Out[6]: 15

Advantages of anonymous functions:

1. When using python to write some scripts, using lambda can save the process of defining functions and make the code more concise.

2. For some abstract functions that will not be reused in other places, sometimes it is also a problem to name a function. Using lambda does not need to consider the problem of naming by hierarchy theory.

3. Use lambda to make the code easier to understand at some time.
lambda Foundation:

In a lambda statement, a colon is preceded by a parameter, and there can be multiple parameters separated by commas. To the right of the colon is the return value.

lambda statement is actually a function object.

help(reduce)

Help on built-in function reduce in module __builtin__:

reduce(...)

reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,

from left to right, so as to reduce the sequence to a single value.

For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates

((((1+2)+3)+4)+5).  If initial is present, it is placed before the items

of the sequence in the calculation, and serves as a default when the

sequence is empty.

(END)

Binary calculation of reduce:

In [19]: def add(x,y):

return x + y

....:

In [20]: add(1,3)

Out[20]: 4

Find the sum of 1 to 100:

#!/usr/bin/python
# -*- coding: utf-8 -*-
#date:2019.07.05
print ('1+100 The sum of:%s' % reduce(lambda x,y:x+y,range(1,101)))

Finding factorial:

#!/usr/bin/python
# -*- coding: utf-8 -*-
#date:2019.07.05
print ('5 The factorials of are:%s' % reduce(lambda x,y:x*y,range(1,6)))

Keywords: Python Lambda ssh shell

Added by thesimon on Fri, 01 Nov 2019 06:26:20 +0200