资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

在Python中递归函数调用举例and匿名函数lambda求1~100的和及计算阶乘举例

1.递归列出目录里的文件的脚本举例
列出目录中的文件可以通过下面方法: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']

判断是否为目录:

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

Out[5]: True

判断是否为文件:

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

Out[7]: True

拼接文件名字的绝对路径:

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

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

列出目录下所有文件脚本如果下:

#!/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.匿名函数lambda

lambda函数是一种快速定义单选的最小函数,可以用在任何需要函数的地方。

3*5实现方法:

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

...:     return x * y

...:

In [2]: fun(3,5)

Out[2]: 15

匿名函数定义如果下:

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

Out[3]: >    #返回的对象

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

In [6]: r(3,5)

Out[6]: 15

匿名函数优点:

1.使用python写一些脚本时候,使用lambda可以省去定义函数的过程,让代码更加精简。

2.对于一些抽象的,不会被别的地方再重复使用的函数,有时候函数起个名字也是个难题,使用lambda不需要层次理论考虑命名的问题。

3.使用lambda在某些时候让代码更容易理解。
lambda基础:

lambda语句中,冒号前是参数,可以有多个,逗号隔开,冒号右边是返回值。

lambda语句构建的其实是一个函数对象。

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)

reduce二元计算:

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

return x + y

....:

In [20]: add(1,3)

Out[20]: 4

求1到100相加的和:

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

求阶乘:

#!/usr/bin/python
# -*- coding: utf-8 -*-
#date:2019.07.05
print ('5的阶乘是:%s' % reduce(lambda x,y:x*y,range(1,6)))

新闻标题:在Python中递归函数调用举例and匿名函数lambda求1~100的和及计算阶乘举例
文章分享:http://cdkjz.cn/article/jcosgg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220