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
倒序输出
L2 = sorted(L1,lambda p1,p2:cmp(p1.name,p2.name),reverse=1)
L2 = sorted(L1,lambda p1,p2:cmp(p1.name,p2.name),reverse=1)
2015-09-24
表示这个例子完全不知所云。。。
1. 本人跑了一遍,f1, f2, f3 根本就不是出数字,而是函数的内存地址,而且是三个地址组成的列表(本人结果:[<function f at 0x02AC4A70>, <function f at 0x02AC4AB0>, <function f at 0x02AC4AF0>])
2. fs.append(f)这句,是把函数f的地址,放进列表中,根本就不是在调用f函数,如果改成f() 才是调用函数
所以根本就不会输出数字,不知作者想表达社么
1. 本人跑了一遍,f1, f2, f3 根本就不是出数字,而是函数的内存地址,而且是三个地址组成的列表(本人结果:[<function f at 0x02AC4A70>, <function f at 0x02AC4AB0>, <function f at 0x02AC4AF0>])
2. fs.append(f)这句,是把函数f的地址,放进列表中,根本就不是在调用f函数,如果改成f() 才是调用函数
所以根本就不会输出数字,不知作者想表达社么
2015-09-24