Chinaunix首页 | 论坛 | 博客
  • 博客访问: 756876
  • 博文数量: 231
  • 博客积分: 3217
  • 博客等级: 中校
  • 技术积分: 2053
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-04 12:01
文章分类

全部博文(231)

文章存档

2015年(1)

2013年(10)

2012年(92)

2011年(128)

分类: LINUX

2012-08-27 15:34:08

http://xiaofeng1982.blog.163.com/blog/static/31572458201093102036385/
map函数func作用于给定序列的每个元素,并用一个列表来提供返回值。
map函数python实现代码:

def map(func,seq): 
    mapped_seq = [] 
    for eachItem in seq: 
        mapped_seq.append(func(eachItem)) 
    return mapped_seq 


filter函数的功能相当于过滤器。调用一个布尔函数bool_func来迭代遍历每个seq中的元素;返回一个使bool_seq返回值为true的元素的序列。
filter函数python代码实现:

def filter(bool_func,seq): 
    filtered_seq = [] 
    for eachItem in seq: 
        if bool_func(eachItem): 
            filtered_seq.append(eachItem) 
    return filtered_seq 


reduce函数,func为二元函数,将func作用于seq序列的元素,每次携带一对(先前的结果以及下一个序列的元素),连续的将现有的结果和下一个值作用在获得的随后的结果上,最后减少我们的序列为一个单一的返回值。
reduct函数python代码实现:

def reduce(bin_func,seq,initial=None): 
    lseq = list(seq) 
    if initial is None: 
        res = lseq.pop(0) 
    else: 
        res = initial 
    for eachItem in lseq: 
        res = bin_func(res,eachItem) 
    return res 


下面是测试的代码

#coding:utf-8
def map_func(lis):
    return lis + 1
def filter_func(li):
    if li % 2 == 0:
        return True
    else:
        return False
        
def reduce_func(li, lis):
    return li + lis
    
li = [1,2,3,4,5]
map_l = map(map_func, li) #将li中所有的数都+1
filter_l = filter(filter_func, li) #得到li中能被2整除的
reduce_l = reduce(reduce_func, li) #1+2+3+4+5
print map_l
print filter_l
print reduce_l

运行结果如下:
C:\>python test1.py
[2, 3, 4, 5, 6]
[2, 4]
15


Python isatty()方法
描述
Python文件操作isatty()方法:此方法返回True,如果该文件被连接到一个tty(类)设备(终端设备),否则返回False。

此方法返回True,如果该文件被连接到一个tty(类)设备(终端设备),否则返回False.


语法:
fileObject.isatty(); 
参数:
下面是详细参数:

NA

例子:
#!/usr/bin/python

# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name

ret = fo.isatty()
print "Return value : ", ret

# Close opend file
fo.close()
这将产生以下结果:

Name of the file:  foo.txt
Return value :  False
阅读(1533) | 评论(0) | 转发(0) |
0

上一篇:c中注意

下一篇:scons中getopt和setopt

给主人留下些什么吧!~~