def __call__(self,num):
a,b,L=0,1,[]
for x in range(num):
if x==0:
L.insert(x,a)
else:
if x==1:
L.insert(x,b)
else:
L.insert(x,L[x-2]+L[x-1])
return L
a,b,L=0,1,[]
for x in range(num):
if x==0:
L.insert(x,a)
else:
if x==1:
L.insert(x,b)
else:
L.insert(x,L[x-2]+L[x-1])
return L
2019-02-15
def __add__(self, r):
x=self.p*r.q+self.q*r.p
y=self.q * r.q
if x<y:
x,y=y,x
while y!=0:
temp=x%y
x=y
y=temp
return Rational((self.p*r.q+self.q*r.p )/x,self.q * r.q/x)
x=self.p*r.q+self.q*r.p
y=self.q * r.q
if x<y:
x,y=y,x
while y!=0:
temp=x%y
x=y
y=temp
return Rational((self.p*r.q+self.q*r.p )/x,self.q * r.q/x)
2019-02-14
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 x: x.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 x: x.name)
print L2[0].name
print L2[1].name
print L2[2].name
2019-02-13
不解释:
···def count():
fs = []
for i in range(1, 4):
def f(i):
def g():
return i * i
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()···
···def count():
fs = []
for i in range(1, 4):
def f(i):
def g():
return i * i
return g
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()···
2019-02-12
import os
print os.path.isdir(r'/data/webroot/resource')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
print os.path.isdir(r'/data/webroot/resource')
print os.path.isfile(r'/data/webroot/resource/python/test.txt')
2019-02-12
很简单的一个问题 这个题目中 并不没有用到 *args, **kw 这两个 只是单纯的让写一个装饰器而已 你既然上面说了*args, **kw 那就运用到题目中 或者详细说明 不要混淆了
2019-02-12
def calc_prod(lst):
return lambda : reduce(lambda x, y : x * y, lst)
f = calc_prod([1, 2, 3, 4])
print f()
return lambda : reduce(lambda x, y : x * y, lst)
f = calc_prod([1, 2, 3, 4])
print f()
2019-02-11
def calc_prod(lst):
def prod(x, y):
return x * y
return lambda : reduce(prod, lst)
f = calc_prod([1, 2, 3, 4])
print f()
def prod(x, y):
return x * y
return lambda : reduce(prod, lst)
f = calc_prod([1, 2, 3, 4])
print f()
2019-02-11
import math
def is_sqr(x):
v = math.sqrt(x)
return v == int(v)
print filter(is_sqr, range(1, 101))
# 若为小数, 则int取整之后, 必然不等, 即可判断.
def is_sqr(x):
v = math.sqrt(x)
return v == int(v)
print filter(is_sqr, range(1, 101))
# 若为小数, 则int取整之后, 必然不等, 即可判断.
2019-02-11
def cmp_ignore_case(s1, s2):
a=s1.lower()
b=s2.lower()
if a<b:
return -1
if a>b:
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
a=s1.lower()
b=s2.lower()
if a<b:
return -1
if a>b:
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2019-01-30
import math
def is_sqr(x):
return (x == math.pow(int(math.sqrt(x)),2))
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return (x == math.pow(int(math.sqrt(x)),2))
print filter(is_sqr, range(1, 101))
2019-01-29