lambda p1, p2: cmp(p1.name, p2.name) 是匿名函数的用法
p1,p2是传入参数,cmp(p1.name,p2.name)是返回值(2-10节的那个实例)
这里猜想cmp函数是用来区分大小写的,不过好像没有实现
不用区分大小写的话这样写就可以了:L2 = sorted(L1,key = lambda x:x.name)
p1,p2是传入参数,cmp(p1.name,p2.name)是返回值(2-10节的那个实例)
这里猜想cmp函数是用来区分大小写的,不过好像没有实现
不用区分大小写的话这样写就可以了:L2 = sorted(L1,key = lambda x:x.name)
2015-08-30
直接把 lambda 函数赋值给 self.get_grade 和绑定方法有所不同,函数调用不需要传入 self,但是方法调用需要传入 self。
2015-08-30
class Fib(object):
def __init__(self, num):
L=[0,1]
for i in range(num-2):
L.append(L[-2]+L[-1])
self.numbers=L
def __str__(self):
return str(self.numbers)
__repr__=__str__
def __len__(self):
return len(self.numbers)
def __init__(self, num):
L=[0,1]
for i in range(num-2):
L.append(L[-2]+L[-1])
self.numbers=L
def __str__(self):
return str(self.numbers)
__repr__=__str__
def __len__(self):
return len(self.numbers)
2015-08-27
def performance(unit):
def per_new(f):
def fn(*args,**kw):
print 'call %s() in %s (%s)' % (f.__name__,time.time(),unit)
return f(*args,**kw)
return fn
return per_new
def per_new(f):
def fn(*args,**kw):
print 'call %s() in %s (%s)' % (f.__name__,time.time(),unit)
return f(*args,**kw)
return fn
return per_new
2015-08-25
...。。。要崩溃死了。。。pip安装不了,easygui也安装不了...明明都是按照老师说的做的,环境变量也弄好了。。。烦死了
2015-08-25
def calc_prod(lst):
def lazy_prod():
def g(x,y):
return x*y
return reduce(g,lst,1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
def g(x,y):
return x*y
return reduce(g,lst,1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2015-08-25
def cmp_ignore_case(s1, s2):
if s1.lower()>s2.lower():
return 1
if s1.lower()==s2.lower():
return 0
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower()>s2.lower():
return 1
if s1.lower()==s2.lower():
return 0
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-08-24
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
2015-08-24
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-08-24
class Person(object):
def __init__(self,name,gender,birth,**kw):
self.name=name
self.gender=gender
self.birth=birth
for k,v in kw.iteritems():
stm="self.%s=\"%s\""%(k,v)
exec(stm)
def __init__(self,name,gender,birth,**kw):
self.name=name
self.gender=gender
self.birth=birth
for k,v in kw.iteritems():
stm="self.%s=\"%s\""%(k,v)
exec(stm)
2015-08-24