print filter((lambda s: True if (s and len(s.strip()) > 0) else False), ['test', None, '', 'str', ' ', 'END'])
2017-09-29
def cmp_ignore_case(s1, s2):
if s1[0].lower()>s2[0].lower():
return 1
if s1[0].lower()<s2[0].lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[0].lower()>s2[0].lower():
return 1
if s1[0].lower()<s2[0].lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2017-09-28
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2017-09-28
class Person(object):
__count = 0
def __init__(self, name):
Person.__count = Person.__count + 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'AttributeError'
标答!!!
__count = 0
def __init__(self, name):
Person.__count = Person.__count + 1
self.name = name
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'AttributeError'
标答!!!
2017-09-28
Python3.x没有cmp函数了
L2 = sorted(L1,key = lambda x:x.name[:1].lower())
L2 = sorted(L1,key = lambda x:x.name[:1].lower())
2017-09-28
def format_name(s):
x=s[0].upper()+s[1:].lower()
return x
print map(format_name, ['adam', 'LISA', 'barT'])
x=s[0].upper()+s[1:].lower()
return x
print map(format_name, ['adam', 'LISA', 'barT'])
2017-09-28
def cmp_ignore_case(s1, s2):
if s1[0].upper()<s2[0].upper():
return -1
if s1[0].upper()>s2[0].upper():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[0].upper()<s2[0].upper():
return -1
if s1[0].upper()>s2[0].upper():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2017-09-28
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2017-09-28
def format_name(s):
return s[0].upper()+s[1:len(s)].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:len(s)].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2017-09-28
import time
def performance(f):
def fn(*args,**kw):
print time.asctime( time.localtime(time.time()) )
return f(*args,**kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
这样也能出来
def performance(f):
def fn(*args,**kw):
print time.asctime( time.localtime(time.time()) )
return f(*args,**kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
这样也能出来
2017-09-27
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print (f1,f2,f3)
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print (f1,f2,f3)
2017-09-27
def count():
fs = []
for i in range(1, 4):
def f(i):
return i*i
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1,f2,f3
问题代码是因为没有调用内层函数,只要调用它就可以了。没必要再写一层函数。
fs = []
for i in range(1, 4):
def f(i):
return i*i
fs.append(f(i))
return fs
f1, f2, f3 = count()
print f1,f2,f3
问题代码是因为没有调用内层函数,只要调用它就可以了。没必要再写一层函数。
2017-09-27
def p(u):
def d(f):
def w(*args,**kw):
if u == 's':
print 'call '+ '() in '+ time.clock().__str__()
if u== 'ms':
print 'call '+ '() in '+ (time.clock()*1000).__str__()
return f(*args,**kw)
return w
return d
def d(f):
def w(*args,**kw):
if u == 's':
print 'call '+ '() in '+ time.clock().__str__()
if u== 'ms':
print 'call '+ '() in '+ (time.clock()*1000).__str__()
return f(*args,**kw)
return w
return d
2017-09-27
import time
def performance(f):
def fn(*args,**kw):
print 'call ' + f.__name__ + '() in ' + time.clock().__str__()
return f(*args,**kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def performance(f):
def fn(*args,**kw):
print 'call ' + f.__name__ + '() in ' + time.clock().__str__()
return f(*args,**kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2017-09-27