这样是不是好理解点,写得太简洁不容易理解,而且容易出错
class Fib(object):
def __call__(self, num):
L = []
m = 0
n = 1
for i in range(0, 10):
L.append(m)
temp = m
m = n
n = temp + n
return L
f = Fib()
print f(10)
class Fib(object):
def __call__(self, num):
L = []
m = 0
n = 1
for i in range(0, 10):
L.append(m)
temp = m
m = n
n = temp + n
return L
f = Fib()
print f(10)
2020-04-19
def calc_prod(lst):
def lazy_prod():
sum=1
for x in lst:
sum=sum*x
return sum
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
sum=1
for x in lst:
sum=sum*x
return sum
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2020-04-17
def cmp_ignore_case(s1, s2):
if s1.upper()>s2.upper():
return 1
if s1.upper()<s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.upper()>s2.upper():
return 1
if s1.upper()<s2.upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2020-04-17
我们打断点可以发现在for循环里根本就没有进入到f()方法里也就是说append(f)没有触发f方法,只是在最后输出的时候才会进入return j*j,但是我们如果改成append(f())就能顺利输出1,4,9了
2020-04-16
if math.sqrt(x)%2==0:
return x
if math.sqrt(x)%2==1:
return x
虽然做法是对的,但是方法感觉有点笨拙
return x
if math.sqrt(x)%2==1:
return x
虽然做法是对的,但是方法感觉有点笨拙
2020-04-15
>>> from functools import reduce
>>> def prod(x,y):
return x*y
>>> print(reduce(prod,[2,3,4,5]))
120
>>> def prod(x,y):
return x*y
>>> print(reduce(prod,[2,3,4,5]))
120
2020-04-15
>>> def format_name(s):
return s[0:1].upper()+s[1:].lower()
>>> print( list(map(format_name, ['adam', 'LISA', 'barT'])))
['Adam', 'Lisa', 'Bart']
return s[0:1].upper()+s[1:].lower()
>>> print( list(map(format_name, ['adam', 'LISA', 'barT'])))
['Adam', 'Lisa', 'Bart']
2020-04-15
def count():
fs = [i*i for i in range(1,4)]
return map(lambda x: lambda y=1: x,fs)
尝试了用更少的代码完成.
fs = [i*i for i in range(1,4)]
return map(lambda x: lambda y=1: x,fs)
尝试了用更少的代码完成.
2020-03-21
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
计算最大公约数
if b == 0:
return a
return gcd(b, a % b)
计算最大公约数
2020-03-20