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'])
2016-03-04
#from os.path import isdir ,isfile
import os
print os.path.isdir(r'/data/webroot/resource/python')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
import os
print os.path.isdir(r'/data/webroot/resource/python')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
2016-03-04
def count():
fs = []
for i in range(1, 4):
def lazy(x):
def calc():
return x * x
return calc
fs.append(lazy(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def lazy(x):
def calc():
return x * x
return calc
fs.append(lazy(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2016-03-04
import functools
sorted_ignore_case = functools.partial(sorted, cmp=lambda s1, s2: cmp(s1.upper(), s2.upper()))
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
sorted_ignore_case = functools.partial(sorted, cmp=lambda s1, s2: cmp(s1.upper(), s2.upper()))
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2016-03-02
请检查输出是否匹配:['about', 'bob', 'credit', 'zoo'],再试试!直接进入下一节
bug
bug
2016-03-02
import time
def performance(unit):
def x(f):
def y(*args,**kw):
t1=time.time()
r=f(*args,**kw)
t2=time.time()
print 'call %s() in %f %s' % (f.__name__, (t2-t1), unit)
return r
return y
return x
def performance(unit):
def x(f):
def y(*args,**kw):
t1=time.time()
r=f(*args,**kw)
t2=time.time()
print 'call %s() in %f %s' % (f.__name__, (t2-t1), unit)
return r
return y
return x
2016-03-02
最赞回答 / ENMENGYI
我的理解是,在程序中调用sorted()函数只能比较int、str等内置数据类型。如果a,b,c都是Student类的实例,在调用sorted(a, b, c)时,由于a,b,c是Student类型的数据,不属于内置类型,因此靠sorted()中的默认cmp函数是无法完成元素之间的比较的。因此,这里要提供给sorted()函数的cmp函数需要在Student类中提供。这相当于告诉sorted()函数,Student数据类型的变量之间应该是怎样比较大小的。
2016-03-02
print filter(lambda s: s and len(s.strip())>0, ['test', None, '', 'str', ' ', 'END'])
2016-03-02