python开发技术详解(五)

文章目录:

  sys.modules

  python程序结构

  python内置函数

  字符串

  时间字符串转换


 

sys.modules

sys.modules 全局变量,其实就是一个字典

zip 

a=(1,3,4,5,6)
b=(3,4,5,6)
c=(1,3,4,5,1)
print zip(a,b,c)

output:

[(1, 3, 1), (3, 4, 3), (4, 5, 4), (5, 6, 5)]

set

a=(1,3,4,5,6)
b=(3,4,5,6,7)

print set(a)|set(b)
print set(a)-set(b)
print set(a)&set(b)

output:

set([1, 3, 4, 5, 6, 7])
set([1])
set([3, 4, 5, 6])

 


 

python内置函数:

 apply #将函数和参数分离开来。  

def sum(x,y):
    return x+y

print apply(sum,(1,3)) 

 map

print map(lambda x:x**2,(1,3,4,5))

fliter 根据函数过滤多余的元素

def judge(x):
    if x>1:
        return True
        

print filter(judge, (1,3,4,6,7))

reduce  循环运算:

def sum(x,y):
    return x+y

print reduce(sum,(1,3,4))   #reduce一定要有两个参数。

字符串

  %s 字符串

  %f 浮点数

  %c 字符

  %d 数字

 


时间字符串转换:

import time

t=time.localtime(time.time())

time.strftime('%Y-%m-%d %H-%M-%S',t); #时间戳转换为字符串

t=time.strptime('2008-08-09', '%Y-%m-%d') #字符串转化为时间

print time.mktime(t)    #structTime转换为时间戳

 

posted @ 2014-10-14 14:59  UCanBeFree  阅读(138)  评论(0)    收藏  举报