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))
2018-09-27
print sorted(['bob', 'about', 'Zoo', 'Credit'], key=lambda x:x.lower())
2018-09-27
def __str__(self):
i=self.p
while i<=self.p:
if(self.p%i==0 and self.q%i==0):
break
i=i-1
return '%s/%s'%(self.p/i,self.q/i)
从两个数中任意一个数开始减,都能除尽的数
i=self.p
while i<=self.p:
if(self.p%i==0 and self.q%i==0):
break
i=i-1
return '%s/%s'%(self.p/i,self.q/i)
从两个数中任意一个数开始减,都能除尽的数
2018-09-27
import math
print filter(lambda x:math.sqrt(x)%1==0, range(1, 101))
print filter(lambda x:math.sqrt(x)%1==0, range(1, 101))
2018-09-27
如何去掉`__xxx__`这类的特殊属性,只保留我们自己定义的属性?回顾一下filter()函数的用法。
list(filter(lambda x: '_' not in x, dir(p)))
list(filter(lambda x: '_' not in x, dir(p)))
2018-09-26
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-09-26
L1 = [p1, p2, p3]
L2 = sorted(L1,key=lambda x:x.name)
L2 = sorted(L1,key=lambda x:x.name)
2018-09-24
class Person(object):
pass
class Student(Person):
pass
class Teacher(Person):
pass
class SkillMixin(object):
pass
我觉得这个题根本不需要这几行代码
pass
class Student(Person):
pass
class Teacher(Person):
pass
class SkillMixin(object):
pass
我觉得这个题根本不需要这几行代码
2018-09-23
def count():
fs = []
def f(i):
def g():return i*i
return g
for i in range(1,4):
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
def f(i):
def g():return i*i
return g
for i in range(1,4):
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-09-22
def calc_prod(lst):
def prod():
return reduce(lambda x,y:x*y,lst,1)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
return reduce(lambda x,y:x*y,lst,1)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-09-22
def cmp_ignore_case(s1, s2):
a = ord(s1[0:1].upper()) - ord(s2[0:1].upper())
return 1 if a > 0 else 0 if a == 0 else -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
a = ord(s1[0:1].upper()) - ord(s2[0:1].upper())
return 1 if a > 0 else 0 if a == 0 else -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-09-21
import math
def is_sqr(x):
return len(str(math.sqrt(x))) < 5
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return len(str(math.sqrt(x))) < 5
print filter(is_sqr, range(1, 101))
2018-09-21
import math
def is_sqr(x):
return (math.sqrt(x)*1.0).is_integer()
#3.0.is_integer() #True
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return (math.sqrt(x)*1.0).is_integer()
#3.0.is_integer() #True
print filter(is_sqr, range(1, 101))
2018-09-20
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-09-20