最赞回答 / Jane_meng
个人理解:在原题目中fs.append(f)是向fs这个list中新增了函数(函数成为了list的元素),并且该函数还并未被调用,也就是没有运行,也就没有计算i*i的值,函数中的变量i会随着for循环而改变,循...
2019-02-18
最新回答 / _啦_啦_啦_
import functoolssorted_ignore_case = functools.partial(sorted,key=str.lower)print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])就像这样
2019-02-18
最赞回答 / VSir0
<...code...>是先计算右边,a = 0, a + b = 1,,然后赋值给左边,a = 0, b = 1如果是<...code...>结果就是a = 1,b = a + b = 1 + 1 = 2了
2019-02-17
def performance(unit):
def per(f):
def fn(*args, **kw):
t1 = datetime.datetime.now()
res = f(*args, **kw)
t2 = datetime.datetime.now()
t = (t2 - t1).total_seconds()
t = t * 1000 if (unit == 'ms') else t
print('call %s() in %f' % (f.__name__, t))
return res
return fn
return per
def per(f):
def fn(*args, **kw):
t1 = datetime.datetime.now()
res = f(*args, **kw)
t2 = datetime.datetime.now()
t = (t2 - t1).total_seconds()
t = t * 1000 if (unit == 'ms') else t
print('call %s() in %f' % (f.__name__, t))
return res
return fn
return per
2019-02-15
print(sorted(['bob', 'about', 'Zoo', 'Credit'], key=lambda x: x.lower()))
2019-02-15
def __call__(self,num):
a,b,L=0,1,[]
for x in range(num):
if x==0:
L.insert(x,a)
else:
if x==1:
L.insert(x,b)
else:
L.insert(x,L[x-2]+L[x-1])
return L
a,b,L=0,1,[]
for x in range(num):
if x==0:
L.insert(x,a)
else:
if x==1:
L.insert(x,b)
else:
L.insert(x,L[x-2]+L[x-1])
return L
2019-02-15
def __add__(self, r):
x=self.p*r.q+self.q*r.p
y=self.q * r.q
if x<y:
x,y=y,x
while y!=0:
temp=x%y
x=y
y=temp
return Rational((self.p*r.q+self.q*r.p )/x,self.q * r.q/x)
x=self.p*r.q+self.q*r.p
y=self.q * r.q
if x<y:
x,y=y,x
while y!=0:
temp=x%y
x=y
y=temp
return Rational((self.p*r.q+self.q*r.p )/x,self.q * r.q/x)
2019-02-14
最赞回答 / 奋斗的面包
在Python 3.x, 取消了 cmp 参数, 只保留了Key Function参数。正确代码如下:12L = [Student('Tim', 99), Student('Bob', 88), Student('Alice', 99)]print (sorted(L, key=lambda student:(student.score,student.name)))#优先按照score,其次按照name
2019-02-14