def calc_prod(lst):
def k():
return reduce(lambda x,y :x*y,lst)
return k
f = calc_prod([1, 2, 3, 4])
print f()
def k():
return reduce(lambda x,y :x*y,lst)
return k
f = calc_prod([1, 2, 3, 4])
print f()
2015-07-02
def cmp_ignore_case(s1, s2):
if s1.lower()<s2.lower():
return -1
if s1.lower()>s2.lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower()<s2.lower():
return -1
if s1.lower()>s2.lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-06-30
import math
def is_sqr(x):
if math.sqrt(x)%1==0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if math.sqrt(x)%1==0:
return x
print filter(is_sqr, range(1, 101))
2015-06-30
最赞回答 / JS000
当调用count()时,开始执行循环语句,i分别变成1,2,3即fs=[f(1),f(2),f(3)],因为f1,f2,f3 = count(),因此f1=f(1),f2=f(2),f3=f(3)。然后执行f1(),f2(),f3(),即分别调用三个函数,由代码可知def f(j): def g(): return j*j return g即f1=f(1)=g,所以f1()=g(),执行g函数,返回j*j,此时j=1 所以f1()=...
2015-06-30
class Person(object):
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for key in kw:
setattr(self,key,kw[key])
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for key in kw:
setattr(self,key,kw[key])
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
2015-06-29
def count():
fs = []
for i in range(1, 4):
def f(i):
def g():
return i*i
return g
r=f(i)
fs.append(r)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(i):
def g():
return i*i
return g
r=f(i)
fs.append(r)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2015-06-29
没有学过最大公约数gcd的哭晕在厕所呀
写了不包含0.1的质数组合
除了余数等于0的除了再打印
不为0的直接打印
心想这么麻烦
看了示例哭晕在测试,看来要回头去先把函数背背
写了不包含0.1的质数组合
除了余数等于0的除了再打印
不为0的直接打印
心想这么麻烦
看了示例哭晕在测试,看来要回头去先把函数背背
2015-06-28
实例代码就是精简
无语
def __cmp__(self, s):
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
else:
if self.name < s.name:
return -1
elif self.name > s.name:
return 1
无语
def __cmp__(self, s):
if self.score < s.score:
return 1
elif self.score > s.score:
return -1
else:
if self.name < s.name:
return -1
elif self.name > s.name:
return 1
2015-06-28
@property
def grade(self):
return 'A' if self.__score >= 80 else 'B' if self.__score >= 60 else 'C'
def grade(self):
return 'A' if self.__score >= 80 else 'B' if self.__score >= 60 else 'C'
2015-06-28