import math
def is_sqr(x):
return math.sqrt(x) in range(1,11)
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x) in range(1,11)
print filter(is_sqr, range(1, 101))
2018-01-17
def calc_prod(lst):
def lazy_prod():
prod=1
for i in lst:
prod*=i
return prod
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
prod=1
for i in lst:
prod*=i
return prod
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-01-17
def __cmp__(self, s):
return -cmp(self.score, s.score) * 10 + cmp(self.name, s.name)
return -cmp(self.score, s.score) * 10 + cmp(self.name, s.name)
2018-01-17
麻烦点,能看到过程
def cmp_ignore_case(s1, s2):
L = sorted([s1[0:1].upper(), s2[0:1].upper()])
print L
if L[0] == s1[0:1].upper():
return -1
if L[1] == s1[0:1].upper():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
def cmp_ignore_case(s1, s2):
L = sorted([s1[0:1].upper(), s2[0:1].upper()])
print L
if L[0] == s1[0:1].upper():
return -1
if L[1] == s1[0:1].upper():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-01-17
def calc_prod(lst):
def cal_product():
return reduce(lambda x, y: x*y, lst)
return cal_product
f = calc_prod([1, 2, 3, 4])
print f()
def cal_product():
return reduce(lambda x, y: x*y, lst)
return cal_product
f = calc_prod([1, 2, 3, 4])
print f()
2018-01-17
def cmp_ignore_case(s1, s2):
if s1.upper() > s2.upper():
return 1
elif s1.upper() < s2.upper():
return -1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper() > s2.upper():
return 1
elif s1.upper() < s2.upper():
return -1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-01-17
import math
def is_sqr(x):
return math.floor(math.sqrt(x)) == math.sqrt(x)
print filter(is_sqr, range(1, 100))
def is_sqr(x):
return math.floor(math.sqrt(x)) == math.sqrt(x)
print filter(is_sqr, range(1, 100))
2018-01-17
def cmp_ignore_case(s1, s2):
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-01-16
from os import path
from os import listdir
print listdir(r'/data/webroot/conf')
print path.isdir(r'/data/webroot/conf')
print path.isfile(r'/data/webroot/conf/app.conf')
from os import listdir
print listdir(r'/data/webroot/conf')
print path.isdir(r'/data/webroot/conf')
print path.isfile(r'/data/webroot/conf/app.conf')
2018-01-16
def cmp_ignore_case(s1, s2):
if s1[0].lower() > s2[0].lower():
return 1
if s1[0].lower() < s2[0].lower():
return -1
if s1[0].lower() > s2[0].lower():
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[0].lower() > s2[0].lower():
return 1
if s1[0].lower() < s2[0].lower():
return -1
if s1[0].lower() > s2[0].lower():
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-01-16
def perf(unit):
def per_dect(f):
def time_cal(*args,**kw):
start=time.clock()
f(*args,**kw)
end=time.clock()-start
if unit=='s':
print 'call '+f.__name__+'()',int(end),'s'
if unit=='ms':
print 'call '+f.__name__+'()',int(round(end)*1000),'ms'
return f(*args,**kw)
return time_cal
return per_dect
def per_dect(f):
def time_cal(*args,**kw):
start=time.clock()
f(*args,**kw)
end=time.clock()-start
if unit=='s':
print 'call '+f.__name__+'()',int(end),'s'
if unit=='ms':
print 'call '+f.__name__+'()',int(round(end)*1000),'ms'
return f(*args,**kw)
return time_cal
return per_dect
2018-01-16
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print f1, f2, f3
为何是对的
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print f1, f2, f3
为何是对的
2018-01-16