python查漏补缺之二(map,reduce,filter,collections之deque,tempfile之TemporaryFile())

第一,map

关于map的用法,我们可以看文档或者百度,如果实在懒,就直接在python下用help(map)吧,反正我是经常这么干,然后根据自己的理解,写写示例,写多了也就掌握了

map(...)
    map(function, sequence[, sequence, ...]) -> list
    
    Return a list of the results of applying the function to the items of
    the argument sequence(s).  If more than one sequence is given, the
    function is called with an argument list consisting of the corresponding
    item of each sequence, substituting None for missing values when not all
    sequences have the same length.  If the function is None, return a list of
    the items of the sequence (or a list of tuples if more than one sequence).

很明显,我们抓主要的看,返回的是列表,map的参数接受一个函数和序列,如果多个序列,则函数中的每个参数关联各个序列,如果函数是None,序列转为列表,下面实践下

>>> map(lambda x:x*x,[1,2,3,4,5])
[1, 4, 9, 16, 25]
>>> map(lambda x,y:x*y,[1,2,3,4,5],[5,4,3,2,1])
[5, 8, 9, 8, 5]
>>> map(None,[1,2,3],[4,5,6])
[(1, 4), (2, 5), (3, 6)]
其中最后一个感觉跟zip的用法差不多,

>>> zip([1,2,3],[4,5,6])
[(1, 4), (2, 5), (3, 6)]

当然,之前一直这么用的,但是我们甚至可以尝试把多个函数放到一个列表中,传入map的第二个参数中

def multiply(x):
        return (x*x)
def add(x):
        return (x+x)

funcs = [multiply, add]
for i in range(5):
    value = list(map(lambda x: x(i), funcs))
    print(value)

# Output:
# [0, 0]
# [1, 2]
# [4, 4]
# [9, 6]
# [16, 8]

第二,reduce 

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)
首先,function有俩参数,怎么调用后面的sequence,也给了例子,后面说如果又initial,会先使用initial+sequence的第一个元素

>>> reduce(lambda x,y:x+y,[2,3,4,5],1)
15
>>> reduce(lambda x,y:x+y,[2,3,4,5])
14
>>> reduce(lambda x,y:x+y,[],1)
1
第三,filter

filter(...)
    filter(function or None, sequence) -> list, tuple, or string
    
    Return those items of sequence for which function(item) is true.  If
    function is None, return the items that are true.  If sequence is a tuple
    or string, return the same type, else return a list.
(END)
首先,functtion得能判断True/False,返回True的item,返回值类型与sequence类型一样

>>> filter(lambda x: x+1,range(1,4))
[1, 2, 3]
>>> filter(lambda x: x,range(1,4))
[1, 2, 3]
因为这个lambda是计算用的,不判断True,相当于一个None

>>> filter(None,[1,2,3])
[1, 2, 3]
>>> def foo(x):
...     return x%2 == 0
... 
>>> filter(foo,[1,2,3,4,5,6])
[2, 4, 6]
这是能判断的,返回偶数的

#########################################################################

续一:我们经常用的是list,dict,tuple,set,这四类内建数据结构,但是在某些情况下不能满足我们日益增(bian)长(tai)的需求,比如,我就要从左边删除元素,sequence的旋转调整等,这些东西看起来很炫,其实用list也可以写,但是要写很多代码,但是又个模块叫collections,我个人理解这个模块的有些类就是对之前提到的四种内建数据结构的补充,而且是高端点的东西,其中deque比较酷,有点类似与队列,支持头部的删除和添加,主要方法有:'append', 'appendleft', 'clear', 'count', 'extend', 'extendleft', 'maxlen', 'pop', 'popleft', 'remove', 'reverse', 'rotate'

>>> from collections import deque
>>> q = deque(['aa','bb','cc'])
>>> q.append(2)
>>> q
deque(['aa', 'bb', 'cc', 2])
>>> q.appendleft(1)
>>> q
deque([1, 'aa', 'bb', 'cc', 2])
>>> q.pop()
2
>>> q.popleft()
1
>>> q.rotate(1)
>>> q
deque(['cc', 'aa', 'bb'])
>>> q.rotate(-1)
>>> q
deque(['aa', 'bb', 'cc'])
其中q.raotate()里面的数字是正数,就是把右边第几个元素移到左边,负数表示把左边第几个移到右边
查看官方文档说,list的插入删除是要改变序列长度,有大量数据的移动,所以复杂度约为O(n),deque的两端插入删除较快,查询较list慢

续二:上面高端的用法看完了,再续一个特别实用的东西,python在文件读写的时候,如果。。其实不是如果,这是真实的项目用的方法,如果我们要执行一个命令,命令返回的信息,要保存下来继续查找或者判断,有两种比较实用的方法:

方法1:标准IO输入输出,执行cmd用N多种方法,但是比较喜欢subprocess模块,因为功能比较全,for example

import subprocess
cmd = ['dmesg -c']
p = subprocess.Popen(''.join(cmd),stdout = subprocess.PIPE,shell=True)
returnCode = p.wait()
if returnCode == 0:
    out = p.stdout.readlines()
    tmp = []
    for line in out:
        line = line.strip()
        tmp.append(line)
    print tmp
只写了一部分代码,很中规中矩,用的就是stdout那部分,主要意思是执行cmd,把返回的信息放入stdout,然后用readline方法读入out,把out处理一下尾部的\n,添加进tmp列表,打印tmp。PS:p.wait()可以等待命令执行完成,returnCode判断状态码是否正确

方法2:临时文件,将cmd放入一个临时文件,临时读取,读取完成自动删除,在路径上也找不到文件,屡试不爽的感觉

import tempfile

cmd = []
tmpfile = tempfile.TemporaryFile()
cmd.append("/usr/sbin/check_lic")
execute(cmd, tmpfile, shell=True)
line = tmpfile.read().strip()
print line
其中execute是自定义的方法,功能就是把cmd的执行结果写入到tmpfile中,具体怎么实现

def execute(cmd, std=None, stdi=None, shell=False):
    try:
        flag= False
        if not std:
            flag= True
            std = tempfile.TemporaryFile()
        proc = subprocess.Popen(cmd, stdout=std, stderr=std, stdin=stdi, close_fds=True, shell=shell)
        ret = proc.wait()
        if flag:
            std.close()
        else: 
            std.seek(0)
        if type(cmd) == list:
            cmd_string = ' '.join(cmd)
        return ret
    except Exception,e:
        print e
        return False
大家可以看到,这里的stdout没有用PIPE,而是用std=tempfile.TemporaryFile()代替,而且官网也不建议用PIPE甚至是shell=True也不建议用。。。有这种功能却不建议用,说明还有些特殊情况会出现阻塞或者死锁的bug,但这并不代表不能用,因为。。。谁写的代码都有bug,放心大胆地用,出问题解决问题就行。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值