def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j * j
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(j):
def g():
return j * j
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-08-08
print sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower)
2015-08-08
def format_name(s):
if isinstance(s,str):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
if isinstance(s,str):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2015-08-07
def cmp_ignore_case(s1, s2):
s1=s1[0].lower()
s2=s2[0].lower()
return cmp(s1, s2)
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
s1=s1[0].lower()
s2=s2[0].lower()
return cmp(s1, s2)
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-08-07
def calc_prod(lst):
def mult(x,y):
return x*y
def res():
return reduce(mult,lst)
return res
f = calc_prod([1, 2, 3, 4])
print f()
def mult(x,y):
return x*y
def res():
return reduce(mult,lst)
return res
f = calc_prod([1, 2, 3, 4])
print f()
2015-08-07
import math
def is_sqr(x):
m=str(math.sqrt(x))
L=m.split('.')
if int(L[1])==0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
m=str(math.sqrt(x))
L=m.split('.')
if int(L[1])==0:
return x
print filter(is_sqr, range(1, 101))
2015-08-06
class Person(object):
address='earth'
count=0
def __init__(self,name):
self.name=name
Person.count+=1
address='earth'
count=0
def __init__(self,name):
self.name=name
Person.count+=1
2015-08-05
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-05
def count():
fs = []
for i in range(1, 4):
def f(i=i):
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(i=i):
return i*i
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-08-05
print filter(lambda x: x == x.strip('__'),dir(p))
输出结果:['age', 'course', 'gender', 'name']
输出结果:['age', 'course', 'gender', 'name']
2015-08-04
import operator
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 = operator.attrgetter('name'))
print L2[0].name
print L2[1].name
print L2[2].name
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 = operator.attrgetter('name'))
print L2[0].name
print L2[1].name
print L2[2].name
2015-08-04
所以关键是看count调用时刻如何在函数的定义中就复制一份i的值,而不是制造一个同样指向i的新指针。看答案最巧妙的是 继续包一层闭包,不过还是略绕,感觉还是利用函数参数默认值初始化这点实现比较好理解一点。
2015-08-03
import functools
sorted_ignore_case = functools.partial(sorted,key=str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
这个方便些!
sorted_ignore_case = functools.partial(sorted,key=str.lower)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
这个方便些!
2015-08-03