def cmp_ignore_case(s1, s2):
if s1.upper()<s2.upper():
return -1
elif s1.upper()==s2.upper():
return 0
else:
return 1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper()<s2.upper():
return -1
elif s1.upper()==s2.upper():
return 0
else:
return 1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-04-13
import math
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
2018-04-13
import math
print(list(filter(lambda x: math.sqrt(x) % 1 == 0, range(1,101))))
print(list(filter(lambda x: math.sqrt(x) % 1 == 0, range(1,101))))
2018-04-13
import time, functools
def performance(unit):
def log_performance(f):
@functools.wraps(f)
def tt(x):
print 'call',f.__name__+'() in',time.time(),unit
return f(x)
return tt
return log_performance
def performance(unit):
def log_performance(f):
@functools.wraps(f)
def tt(x):
print 'call',f.__name__+'() in',time.time(),unit
return f(x)
return tt
return log_performance
2018-04-12
import math
def is_sqr(x):
return x%math.sqrt(x)==0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return x%math.sqrt(x)==0
print filter(is_sqr, range(1, 101))
2018-04-12
def count():
fs = []
for i in range(1, 4):
fs.append(lambda x=i: x*x)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
感觉你们的代码好渣,多了解点lambda吧,打包循环变量
fs = []
for i in range(1, 4):
fs.append(lambda x=i: x*x)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
感觉你们的代码好渣,多了解点lambda吧,打包循环变量
2018-04-11
def performance(unit):
def func(f):
def was(*args,**kw):
print('call %s() %s'%(f.__name__,unit))
return f(*args,**kw)
return was
return func
def func(f):
def was(*args,**kw):
print('call %s() %s'%(f.__name__,unit))
return f(*args,**kw)
return was
return func
2018-04-11
def gcd(x, y):
if x < y:
x, y = y, x
while y:
x, y = y, x % y
return x
class Rational(object):
def __init__(self, p, q):
g = gcd(p, q)
self.p, self.q = p / g, q / g
def __str__(self):
return '%d/%d' % (self.p, self.q)
其他一样
if x < y:
x, y = y, x
while y:
x, y = y, x % y
return x
class Rational(object):
def __init__(self, p, q):
g = gcd(p, q)
self.p, self.q = p / g, q / g
def __str__(self):
return '%d/%d' % (self.p, self.q)
其他一样
2018-04-11
难道不是这么写吗 报错
def __cmp__(self, s):
if self.score < s.score:
return -1
elif self.score > s.score:
return 1
else:
return 0
def __cmp__(self, s):
if self.score < s.score:
return -1
elif self.score > s.score:
return 1
else:
return 0
2018-04-11