python学习—Day35-1—匿名函数、内建函数
来源:互联网 发布:剑灵刺客捏脸数据图 编辑:程序博客网 时间:2024/06/05 15:55
递归列出目录里的文件:
In [2]: import os
In [3]: os.listdir('/home')
Out[3]: ['nagios', 'xiaojingjing']
In [4]: os.path.isdir('/home')
Out[4]: True
In [5]: os.path.isdir('/h')
Out[5]: False
In [6]: os.path.isfile('/home/nagios')
Out[6]: False
In [7]: os.path.isfile('/etc/passwd')
Out[7]: True
In [3]: os.listdir('/home')
Out[3]: ['nagios', 'xiaojingjing']
In [4]: os.path.isdir('/home')
Out[4]: True
In [5]: os.path.isdir('/h')
Out[5]: False
In [6]: os.path.isfile('/home/nagios')
Out[6]: False
In [7]: os.path.isfile('/etc/passwd')
Out[7]: True
In [8]: os.path.join('/etc/', 'abcd') \\仅仅将两个字符串连接,并不判断该文件是否存在。
Out[8]: '/etc/abcd'
脚本:通过递归函数
[root@localhost xiaojingjing]# cat digui.py
#!/usr/bin/python
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))]\\isdir判断参数是否为目录,返回为布尔值
files = [i for i in lsdir if os.path.isfile(os.path.join(path, i))]\\isdir判断参数是否为文件,返回为布尔值
if files:
for f in files:
print os.path.join(path, f) \\返回值一定要加上路径,否则找不到文件。
if dirs:
for d in dirs:
print_files(os.path.join(path, d))\\子目录递归调用函数。一般会将紫色部分与绿色部分对调。返回结果的变化为:目录由最深层次开始输出。
print_files(sys.argv[1])
[root@localhost xiaojingjing]# python digui.py /home/
/home/nagios/.bash_logout
/home/nagios/.bashrc
/home/nagios/.bash_profile
/home/xiaojingjing/.bash_logout
/home/xiaojingjing/.bashrc
/home/xiaojingjing/.bash_profile
/home/xiaojingjing/digui.py
#!/usr/bin/python
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))]\\isdir判断参数是否为目录,返回为布尔值
files = [i for i in lsdir if os.path.isfile(os.path.join(path, i))]\\isdir判断参数是否为文件,返回为布尔值
if files:
for f in files:
print os.path.join(path, f) \\返回值一定要加上路径,否则找不到文件。
if dirs:
for d in dirs:
print_files(os.path.join(path, d))\\子目录递归调用函数。一般会将紫色部分与绿色部分对调。返回结果的变化为:目录由最深层次开始输出。
print_files(sys.argv[1])
[root@localhost xiaojingjing]# python digui.py /home/
/home/nagios/.bash_logout
/home/nagios/.bashrc
/home/nagios/.bash_profile
/home/xiaojingjing/.bash_logout
/home/xiaojingjing/.bashrc
/home/xiaojingjing/.bash_profile
/home/xiaojingjing/digui.py
匿名函数:lambda
匿名函数
- lambda函数是一种快速定义单行的最小函数,可以用在任何需要函数的地方。
In [9]: def fun(x, y):
...: return x * y
...:
In [10]: fun(3, 9)
Out[10]: 27
In [11]: r = lambda x, y:x * y
In [12]: r(7, 8)
Out[12]: 56
...: return x * y
...:
In [10]: fun(3, 9)
Out[10]: 27
In [11]: r = lambda x, y:x * y
In [12]: r(7, 8)
Out[12]: 56
匿名函数优点:
- 1.使用python写一些脚本时,使用lambda可以省去定义函数的过程,让代码更加精简。
- 2. 对于一些抽象的,不会被别的地方再重复使用的函数,有时候函数起个名字也是个难题,使用lambda不需要考虑命名的问题。
- 3. 使用lambda在某些时候让代码更容易理解。
lambda语句中,冒号前是参数,可以有多个,逗号隔开,冒号右边是返回值。
lambda语句构建的其实是一个函数对象
In [15]: r
Out[15]: <function __main__.<lambda>> \\带尖括号的就是函数。
In [16]: fun
Out[16]: <function __main__.fun>
Out[15]: <function __main__.<lambda>> \\带尖括号的就是函数。
In [16]: fun
Out[16]: <function __main__.fun>
In [17]: reduce(fun, range(1, 5))
Out[17]: 24
In [18]: reduce(lambda x, y:x + y, range(1, 101)) \\内置函数,对序列进行操作。具体操作是根据函数的参数定义进行。
Out[18]: 5050
Out[17]: 24
In [18]: reduce(lambda x, y:x + y, range(1, 101)) \\内置函数,对序列进行操作。具体操作是根据函数的参数定义进行。
Out[18]: 5050
内置函数:
使用ipython查看内置函数:(不止是函数还有类(多个单词首字母是大写的))
In [2]: __builtin__.
Display all 137 possibilities? (y or n)
__builtin__.ArithmeticError __builtin__.complex
__builtin__.AssertionError __builtin__.copyright
__builtin__.AttributeError __builtin__.credits
__builtin__.BaseException __builtin__.delattr
__builtin__.BufferError __builtin__.dict
__builtin__.BytesWarning __builtin__.dir
__builtin__.DeprecationWarning __builtin__.divmod等等
Display all 137 possibilities? (y or n)
__builtin__.ArithmeticError __builtin__.complex
__builtin__.AssertionError __builtin__.copyright
__builtin__.AttributeError __builtin__.credits
__builtin__.BaseException __builtin__.delattr
__builtin__.BufferError __builtin__.dict
__builtin__.BytesWarning __builtin__.dir
__builtin__.DeprecationWarning __builtin__.divmod等等
In [2]: help(__builtin__.EOFError) \\这个就是类
In [3]: help(__builtin__.divmod) \\这个就是函数
In [3]: help(__builtin__.divmod) \\这个就是函数
内置函数返回数字的绝对值:abs()
首先可以自己写一个一样功能的函数:
In [3]: def fun(x):
...: if x < 0:
...: return -x
...: return x
...:
In [4]: fun(-1)
Out[4]: 1
In [5]: fun(7)
Out[5]: 7
...: if x < 0:
...: return -x
...: return x
...:
In [4]: fun(-1)
Out[4]: 1
In [5]: fun(7)
Out[5]: 7
取列表的最大值最小值:
In [8]: max([1, 2, 9, 11]) \\后面的参数要是可迭代对象,包括:字符串、列表、元组
Out[8]: 11
In [9]: min([1, 2, 9, 11])
Out[9]: 1
Out[8]: 11
In [9]: min([1, 2, 9, 11])
Out[9]: 1
其他常用内置函数:
len():取序列(列表元组)、字典 长度
divmod():返回类型为元组。返回的是上个数字的商和余数
pow():两个参数是两个参数的乘方。三个参数就是前两个乘方与第三个参数取余
In [10]: pow(2, 3)
Out[10]: 8
In [11]: pow(2, 3, 4)
Out[11]: 0
In [12]: pow(2, 3, 3)
Out[12]: 2
Out[10]: 8
In [11]: pow(2, 3, 4)
Out[11]: 0
In [12]: pow(2, 3, 3)
Out[12]: 2
round():返回类型为浮点数。把数字进行四舍五入。第二个参数可选表示保留几位小数。
In [13]: print round(12.58)
13.0
In [14]: print round(12.58, 2)
12.58
In [15]: print round(12.11, 2)
12.11
13.0
In [14]: print round(12.58, 2)
12.58
In [15]: print round(12.11, 2)
12.11
In [16]: print float(24.45)
24.45
In [17]: float(24.45)
Out[17]: 24.449999999999999
24.45
In [17]: float(24.45)
Out[17]: 24.449999999999999
callable():判断一个对象是否是可以调用的(这里就是指函数、类等。)。
type():查看对象类型。
isinstance():判断对象是否是给定类型的。
In [18]: s = '123454'
In [19]: isinstance(s, int)
Out[19]: False
In [20]: isinstance(s, (str, int))
Out[20]: True
In [19]: isinstance(s, int)
Out[19]: False
In [20]: isinstance(s, (str, int))
Out[20]: True
cmp():比较两个对象,cmp(x, y),x大返回正数,相等返回0,x小返回负数。数字、字符串都是可以的。
range():返回类型是数字列表。
xrange():返回类型是对象。功能与range形同。但是xrange性能更好,占用内存少。
类型转换函数:
int():
long():
float():
complex():转换为复数。
str()
list()
tuple()
hex():转换为十六进制,参数为整数或长整形
oct():八进制
chr():返回类型为字符串,字符串只有一个字符。ascii码所对应的数字。
In [23]: chr(1)
Out[23]: '\x01'
In [24]: chr(97)
Out[24]: 'a'
In [25]: chr(65)
Out[25]: 'A'
Out[23]: '\x01'
In [24]: chr(97)
Out[24]: 'a'
In [25]: chr(65)
Out[25]: 'A'
ord():与chr相反。返回的是整数。
In [26]: ord('a')
Out[26]: 97
In [27]: ord('A')
Out[27]: 65
Out[26]: 97
In [27]: ord('A')
Out[27]: 65
eval():结构:eval(hex(10)),把字符串当作有效表达式求值。
In [21]: eval("['a', 'b', 1]")
Out[21]: ['a', 'b', 1]
In [22]: type(eval("['a', 'b', 1]")) \\最外层必须是双引号。
Out[22]: list
Out[21]: ['a', 'b', 1]
In [22]: type(eval("['a', 'b', 1]")) \\最外层必须是双引号。
Out[22]: list
(三)字符串处理的函数。或者叫字符串的方法。
str.capitalize():首字母大写。
str.replace():替换指定字符。
str.split():切分。默认按照 空格 换行符 tab键 切分。将字符串切分为列表。也可以使用参数指定切分标准和切分次数。
str.join():参数需要是可迭代的,返回字符串。将可迭代对象通过本身的字符串相连接。
In [29]: s1 = 'abc'
In [30]: s1.join('12')
Out[30]: '1abc2'
In [31]: s1.join('1245')
Out[31]: '1abc2abc4abc5'
In [30]: s1.join('12')
Out[30]: '1abc2'
In [31]: s1.join('1245')
Out[31]: '1abc2abc4abc5'
string模块:
import string
内置函数:2.4
阅读全文
0 0
- python学习—Day35-1—匿名函数、内建函数
- python学习—Day35—函数
- python学习——匿名函数
- python学习-匿名函数
- Python内建函数—isinstance()函数
- python---内建函数学习
- python内建函数学习
- 【Python】学习笔记——-5.3、匿名函数
- python学习笔记(21)——匿名函数
- Python学习笔记-匿名函数
- 【Python学习日记】匿名函数
- python学习记录---匿名函数
- Python学习 – 匿名函数
- 复习贴-匿名函数,内建函数
- Python的匿名函数——lambda
- python 匿名函数——lamda
- Python 基础 —— 匿名函数 lambda
- Python学习笔记(1) -- Python内建函数文档
- 浅谈dup和dup2的用法
- 编写程序的目的?为什么要学习编程
- 喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以喝多少汽水。
- spring常见面试题
- Android的设计模式-备忘录模式
- python学习—Day35-1—匿名函数、内建函数
- camera 拍照流程
- 数据结构-图-最短路径(1)迪杰斯特拉算法构造
- samba共享
- 【安全牛学习笔记】ACUNETIX WEB VULNERABILITY SCANNER
- 文章内容过长显示省略号
- C语言str系列函数
- 机器学习(7)-EM算法
- github每次push代码时都要输入用户名和密码的解决方法