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'])
2020-08-20
python 3
def cmp_ignore_case(s1,s2):
return 1 if s1.lower()[0] > s2.lower()[0] else -1
def cmp_ignore_case(s1,s2):
return 1 if s1.lower()[0] > s2.lower()[0] else -1
2020-08-12
sorted function 的list 在前,lambda 在后,顺序 和 filter function 相反。这个有什么区别吗
2020-08-05
python3中sorted函数和python2的使用方法不一样,下面是python3的使用方法:
def cmp_ignore_case(x):
return x.upper()
print ( sorted(['bob', 'about', 'Zoo', 'Credit'], key = cmp_ignore_case) )
def cmp_ignore_case(x):
return x.upper()
print ( sorted(['bob', 'about', 'Zoo', 'Credit'], key = cmp_ignore_case) )
2020-07-24
def count():
fs = []
for i in range(1, 4):
def f(k=i):
return k*k
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(k=i):
return k*k
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2020-07-22
def sqrt(x):
return x**0.5
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, sqrt)
return x**0.5
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, sqrt)
2020-07-21
__init__(self, num):
self.num = num
self.l = []
a = 0
b = 1
for i in range(num):
self.l.append(a)
a = a + b
b = a - b
__str__(self):
return str(self.l)
__len__(self):
return len(self.l)
self.num = num
self.l = []
a = 0
b = 1
for i in range(num):
self.l.append(a)
a = a + b
b = a - b
__str__(self):
return str(self.l)
__len__(self):
return len(self.l)
2020-07-09
import time
def performance(f):
def t(*args):
t1=time.time()
f(*args)
t2=time.time()
print 'call '+ f.__name__ +'() in',t2-t1
return f(*args)
return t
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def performance(f):
def t(*args):
t1=time.time()
f(*args)
t2=time.time()
print 'call '+ f.__name__ +'() in',t2-t1
return f(*args)
return t
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2020-06-21
正确答案
import json
class Students(object):
def __init__(self):
self.read()
def read(self):
return r'["Tim", "Bob", "Alice"]'
s = Students()
print (json.load(s))
import json
class Students(object):
def __init__(self):
self.read()
def read(self):
return r'["Tim", "Bob", "Alice"]'
s = Students()
print (json.load(s))
2020-06-21
def calc_prod(lst):
def lazy_prod():
def prod(x,y):
return x*y
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
def prod(x,y):
return x*y
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2020-06-21
import math
def is_sqr(x):
t =math.sqrt(x)
if t % (int(t)) ==0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
t =math.sqrt(x)
if t % (int(t)) ==0:
return x
print filter(is_sqr, range(1, 101))
2020-06-20