import functools
# sorted_ignore_case = functools.partial(sorted,key=lambda str : str.lower())
sorted_ignore_case = functools.partial(sorted,cmp=lambda str1, str2 : 1 if str1.lower() > str2.lower() else -1)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
# sorted_ignore_case = functools.partial(sorted,key=lambda str : str.lower())
sorted_ignore_case = functools.partial(sorted,cmp=lambda str1, str2 : 1 if str1.lower() > str2.lower() else -1)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2018-03-04
From Python 3.0 changelog;
The StringIO and cStringIO modules are gone. Instead, import the io module and use io.StringIO or io.BytesIO for text and data respectively.
The StringIO and cStringIO modules are gone. Instead, import the io module and use io.StringIO or io.BytesIO for text and data respectively.
2018-03-04
import time
def performance(unit):
def performance_decorator(f):
def wrapper(*args, **kw):
print "call %s() in %s%s" % (f.__name__, time.time, unit)
return f(*args, **kw)
return wrapper
return performance_decorator
def performance(unit):
def performance_decorator(f):
def wrapper(*args, **kw):
print "call %s() in %s%s" % (f.__name__, time.time, unit)
return f(*args, **kw)
return wrapper
return performance_decorator
2018-03-03
def calc_prod(lst):
def mult(x, y):
return x * y
def prod():
return reduce(mult, lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def mult(x, y):
return x * y
def prod():
return reduce(mult, lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-03-03
顺便复习下reduce()函数hh
def calc_prod(lst):
def prod(x,y):
return x*y
def multip():
print reduce(prod,lst)
return multip
f = calc_prod([1, 2, 3, 4])
print f()
def calc_prod(lst):
def prod(x,y):
return x*y
def multip():
print reduce(prod,lst)
return multip
f = calc_prod([1, 2, 3, 4])
print f()
2018-03-03
import math
def add(x, y, f):
return f(x) + f(y)
def f(z):
return math.sqrt(z)
print add(25, 9,f)
def add(x, y, f):
return f(x) + f(y)
def f(z):
return math.sqrt(z)
print add(25, 9,f)
2018-03-02
sort()是默认是数字值,字符串ASCII值从小到大的排序,即sort([......],1)
字符一个一个比较,出现不同则停止比较
reseversed_cmp取值是前面的List,感到懵逼多半是这里记不住,理解时很伤啊....
字符一个一个比较,出现不同则停止比较
reseversed_cmp取值是前面的List,感到懵逼多半是这里记不住,理解时很伤啊....
2018-02-28