1.f=lambda x,y:x+y 使用lambda創造乙個匿名函式,也可以給這個匿名函式賦值
3.f=lambda x:x**2
a=[1,2,3]
b=map(f,a)
print b
使用函式迴圈呼叫列表中的元素
4.f=lambda x:x%2==0
a=range(10)
b=filter(f,a)
print b
使用filter函式過濾引數a中的元素呼叫f函式時返回false的元素
5.f=lambda x,y:x+y
a=range(10)
b=reduce(f,a)
print b
使用reduce可以使用f函式進行累計處理
6.查詢內建函式dir(__builtins__)
8.時間的使用
import datetime
today=datetime.datetime.today()
strtoday=today.strftime("%y-%m-%d %h:%m:%s")
print strtoday
a=today.strptime(strtoday,"%y-%m-%d %h:%m:%s")
print a
t=datetime.timedelta(days=1)
yestorday=a-t
print yestorday
9.reduce的使用
a=[1,2,3,4]
import operator
b=reduce(operator.add,a)
print b
10.列表推導式
x=[i for i in range(10) if i%2==0]
def text(i):
if i<10:
return '0%d'%i
else:
return '%d'%i
x=[text(i) i in range(13) if i>0]
11.struct
1)pack,unpack的使用
a=1;b=2;c=3
import struct
d=struct.pack("ihb",a,b,c)
print d
e=struct.unpack("ihb",d)
print e
其中i,h,b再python都是integer型,不過長度不同,對應不同的c型別
可以使用struct.calcsize("i")來計算i的長度
同時還可以使用!,<,>,@,=來安裝不同的順序生成自己。
d=struct.pack("!ihb",a,b,c)
print d
可以看到跟預設的生成的不同
2)pack_into,unpack_from
import ctypes import create_string_buffer
b=create_string_buffer(12)
struct.pack_into("iii",b,0,1,2,3)
b.raw
c=struct.unpack_from("iii",b,0)
c12 enumerate
a=range(10)
for i,j in enumerate(a):
print i,j
python中經常使用的字典內建函式
2 hash obj 返回obj的雜湊值 mydict len mydict 2 hash name 15034981 3 dict.copy 返回字典 淺複製 的乙個副本 mydict yourdict mydict.copy yourdict id mydict 41816664l id you...
學習Python 中經常遇到疑惑的地方
可以這樣,不用儲存遞迴中的變數 import os deffindfile str dir os.path.abspath for x in os.listdir dir if os.path.isdir os.path.join dir,x findfile str,os.path.join di...
Python 語言中經常有疑惑的地方
可以這樣,不用儲存遞迴中的變數 import os def findfile str,dir os.path.abspath for x in os.listdir dir if os.path.isdir os.path.join dir,x findfile str,os.path.join d...