老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

在Python中遞歸函數(shù)調(diào)用舉例and匿名函數(shù)lambda求1~100的和及計(jì)算階乘舉例

1.遞歸列出目錄里的文件的腳本舉例
列出目錄中的文件可以通過下面方法:os.listdir()

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),大洼企業(yè)網(wǎng)站建設(shè),大洼品牌網(wǎng)站建設(shè),網(wǎng)站定制,大洼網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,大洼網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

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.匿名函數(shù)lambda

lambda函數(shù)是一種快速定義單選的最小函數(shù),可以用在任何需要函數(shù)的地方。

3*5實(shí)現(xiàn)方法:

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

...:     return x * y

...:

In [2]: fun(3,5)

Out[2]: 15

匿名函數(shù)定義如果下:

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

匿名函數(shù)優(yōu)點(diǎn):

1.使用python寫一些腳本時候,使用lambda可以省去定義函數(shù)的過程,讓代碼更加精簡。

2.對于一些抽象的,不會被別的地方再重復(fù)使用的函數(shù),有時候函數(shù)起個名字也是個難題,使用lambda不需要層次理論考慮命名的問題。

3.使用lambda在某些時候讓代碼更容易理解。
lambda基礎(chǔ):

lambda語句中,冒號前是參數(shù),可以有多個,逗號隔開,冒號右邊是返回值。

lambda語句構(gòu)建的其實(shí)是一個函數(shù)對象。

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二元計(jì)算:

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)))

新聞標(biāo)題:在Python中遞歸函數(shù)調(diào)用舉例and匿名函數(shù)lambda求1~100的和及計(jì)算階乘舉例
本文鏈接:http://www.xueling.net.cn/article/jcosgg.html

其他資訊

在線咨詢
服務(wù)熱線
服務(wù)熱線:028-86922220
TOP
主站蜘蛛池模板: 91免费国产精品 | 一区二区三区国产欧美日韩 | 亚洲免费成人在线视频 | 精品国产31久久久久久 | 久久成人免费精品网站 | 亚洲国产精品人人做人人爱 | 逼逼综合网| 福利综合网 | 伊人久久大香线蕉影院 | 国内丰满少妇猛烈精品播 | 免费看黄色视频 | 国产主播户外勾搭人xx | 日本一区二区三区免费播放视频了 | 国产乱插 | 乱人伦中文字幕在线 | 538国产精品一区二区免费视频 | 人人妻一区二区三区 | 亚洲精品无码MA在线观看 | 蜜臀性色AV免费 | 丰满少妇好紧多水视频 | 我和隔壁的少妇人妻HD | 成人av在线网 | xvideos国产在线观看 | 欧美成人免费草草影院视频 | 亚洲另类视频 | 久久久免费精品国产一区二区 | 精品熟人一区二区三区四区 | 一级爽片 | 91看片在线?看 | 67194成人手机在线 | 国产中文av在线 | 国产偷抇久久精品a片蜜臀a | 999一区二区三区 | 91麻豆精品| 亚洲国产一区精品 | 51国偷自产一区二区三区的 | 色偷偷2019免费视频观看 | av无线看 | 中文字幕一级毛片 | 精品久久久免费 | 婷婷综合缴情亚洲AV |