第二种:L2=sorted(L1,key=lambda m:m.name)
print L2[0].name
print L2[1].name
print L2[2].name
print L2[0].name
print L2[1].name
print L2[2].name
2015-09-26
L1 = [p1, p2, p3]
L2=sorted([L1[0].name,L1[1].name,L1[2].name])
print L2[0]
print L2[1]
print L2[2]
L2=sorted([L1[0].name,L1[1].name,L1[2].name])
print L2[0]
print L2[1]
print L2[2]
2015-09-26
import math
def is_sqr(x):
return x and float(math.sqrt(x))-int(math.sqrt(x)) == 0
print filter(is_sqr, range(0, 101))
呵呵
def is_sqr(x):
return x and float(math.sqrt(x))-int(math.sqrt(x)) == 0
print filter(is_sqr, range(0, 101))
呵呵
2015-09-25
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2015-09-25
def format_name(s):
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2015-09-25
import math
def is_sqr(x):
return x and (math.sqrt(x)) % 1 == 0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return x and (math.sqrt(x)) % 1 == 0
print filter(is_sqr, range(1, 101))
2015-09-25
class Person(object):
def __init__(self,name, gender, birth,job='**keywords',):
self.name=name
self.gender=gender
self.birth = birth
self.job = job
def __init__(self,name, gender, birth,job='**keywords',):
self.name=name
self.gender=gender
self.birth = birth
self.job = job
2015-09-25
正确使用闭包,就要确保引用的局部变量在函数返回后不能变,所以需要再定义一个内部循环,保证外部循环i的时候,i的数值不会因为内层循环的最后一次赋值导致i=9
不知道理解的对不对
不知道理解的对不对
2015-09-25
import functools
sorted_ignore_case = functools.partial(sorted,key=str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
sorted_ignore_case = functools.partial(sorted,key=str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2015-09-25
class Person(object):
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming == xiaohong
pass
xiaoming = Person()
xiaohong = Person()
print xiaoming
print xiaohong
print xiaoming == xiaohong
2015-09-25
接上一个!!!!!!!!!!!!!!!
print 'call'+f.__name__+'%fs'%(t2-t1)+uint
return f(*args,**kw)
return wrapper
return fs
@performance('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial.__name__
print 'call'+f.__name__+'%fs'%(t2-t1)+uint
return f(*args,**kw)
return wrapper
return fs
@performance('ms')
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial.__name__
2015-09-25
def performance(unit):
def fs(f):
@functools.wraps(f)
def wrapper(*args,**kw):
t1=time.time()
t2=time.time()
def fs(f):
@functools.wraps(f)
def wrapper(*args,**kw):
t1=time.time()
t2=time.time()
2015-09-25
import time
def performance(unit):
def fs(f):
def wrapper(*args,**kw):
t1=time.time()
t2=time.time()
print 'call '+f.__name__+'() %fs'%(t2-t1)+unit
return f(*args,**kw)
return wrapper
return fs
@performance('ms')
....
def performance(unit):
def fs(f):
def wrapper(*args,**kw):
t1=time.time()
t2=time.time()
print 'call '+f.__name__+'() %fs'%(t2-t1)+unit
return f(*args,**kw)
return wrapper
return fs
@performance('ms')
....
2015-09-25