def calc_prod(lst):
def mul(x, y):
return x * y
def lazy_prod():
return reduce(mul, lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def mul(x, y):
return x * y
def lazy_prod():
return reduce(mul, lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2017-12-28
import math
def is_sqr(x):
return x and ( math.sqrt(x) == math.sqrt(x) // 1 )
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return x and ( math.sqrt(x) == math.sqrt(x) // 1 )
print filter(is_sqr, range(1, 101))
2017-12-28
def format_name(s):
n1 = s.lower()
n2 = n1.title()
return n2
print map(format_name, ['adam', 'LISA', 'barT'])
n1 = s.lower()
n2 = n1.title()
return n2
print map(format_name, ['adam', 'LISA', 'barT'])
2017-12-28
class Person(object):
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1, key=lambda s: s.name)
print L2[0].name
print L2[1].name
print L2[2].name
pass
p1 = Person()
p1.name = 'Bart'
p2 = Person()
p2.name = 'Adam'
p3 = Person()
p3.name = 'Lisa'
L1 = [p1, p2, p3]
L2 = sorted(L1, key=lambda s: s.name)
print L2[0].name
print L2[1].name
print L2[2].name
2017-12-27
import os
print os.path.isdir(r'/data/webroot/')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
引入模块 并调用模块中函数时 要调用全名
print os.path.isdir(r'/data/webroot/')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
引入模块 并调用模块中函数时 要调用全名
2017-12-27
import functools
sorted_ignore_case = functools.partial(sorted,key=lambda k:k.lower())
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
这里很奇怪,参数名称只能用key,用别的字符就会报错
sorted_ignore_case = functools.partial(sorted,key=lambda k:k.lower())
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
这里很奇怪,参数名称只能用key,用别的字符就会报错
2017-12-27
class Person(object):
count = 0
def __init__(self,name):
Person.count += 1
self.name = name
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
count = 0
def __init__(self,name):
Person.count += 1
self.name = name
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
2017-12-27
def performance(unit):
def a(f):
@functools.wraps(f)
def b(*args, **kw):
c = f(*args, **kw)
if unit == 'ms':
print '1'
else:
print '2'
return c
return b
return a
def a(f):
@functools.wraps(f)
def b(*args, **kw):
c = f(*args, **kw)
if unit == 'ms':
print '1'
else:
print '2'
return c
return b
return a
2017-12-27
import time
import datetime
def performance(unit):
def a(f):
def b(*args):
c=f(*args)
if unit == 'ms':
print datetime.datetime.now()
else:
print time.time()
return c
return b
return a
import datetime
def performance(unit):
def a(f):
def b(*args):
c=f(*args)
if unit == 'ms':
print datetime.datetime.now()
else:
print time.time()
return c
return b
return a
2017-12-27
import time
def performance(f):
def a(*args):
b = f(*args)
print 'call factorial() in'+str(time.time())
return b
return a
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def performance(f):
def a(*args):
b = f(*args)
print 'call factorial() in'+str(time.time())
return b
return a
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2017-12-27
"print f" can write as "list f"
that can do the function as "__str__()"
that can do the function as "__str__()"
2017-12-27
def cmp_ignore_case(s1, s2):
if s1.lower() > s2.lower():
return 1
elif s1.lower() < s2.lower():
return -1
else:
return 0
if s1.lower() > s2.lower():
return 1
elif s1.lower() < s2.lower():
return -1
else:
return 0
2017-12-26