print (list(filter(lambda x:x and x.strip(), ['test', None, '', 'str', ' ', 'END'])))
其实我觉得直接一个判断就够了:
print (list(filter(lambda x: x.strip(), ['test', None, '', 'str', ' ', 'END'])))
其实我觉得直接一个判断就够了:
print (list(filter(lambda x: x.strip(), ['test', None, '', 'str', ' ', 'END'])))
2018-12-08
def format_name(s):
return s.lower().title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.lower().title()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-12-05
class Rational(object):
def __init__(self, p, q):
self.p = p
self.q = q
def __int__(self):
return self.p // self.q
def __float__(self):
return float(self.p)/self.q
print float(Rational(7, 2))
print float(Rational(1, 3))
def __init__(self, p, q):
self.p = p
self.q = q
def __int__(self):
return self.p // self.q
def __float__(self):
return float(self.p)/self.q
print float(Rational(7, 2))
print float(Rational(1, 3))
2018-12-03
def gcd(p,q):
key=1
min = p if p<q else q
for i in range(2,min+1):
if p%i==0 and q%i==0:
key=i
break
else:
continue
return key
key=1
min = p if p<q else q
for i in range(2,min+1):
if p%i==0 and q%i==0:
key=i
break
else:
continue
return key
2018-12-03
import functools
def sorted2(l):
t=[]
for i in l:
t.append(i.lower())
return sorted(t)
sorted_ignore_case = functools.partial(sorted2)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
def sorted2(l):
t=[]
for i in l:
t.append(i.lower())
return sorted(t)
sorted_ignore_case = functools.partial(sorted2)
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2018-12-02
import time
def performance(f):
def fn(x):
t1= time.time()
f(x)
t2=time.time()
print 'call %s() in %fs' % (f.__name__,(t2-t1))
return f(x)
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(x):
t1= time.time()
f(x)
t2=time.time()
print 'call %s() in %fs' % (f.__name__,(t2-t1))
return f(x)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2018-12-02
class Fib(object):
def __call__(self, num) :
L = [0,1]
for x in range(2,num):
L.append(L[x-1] + L[x-2])
return L
f = Fib()
print f(10)
def __call__(self, num) :
L = [0,1]
for x in range(2,num):
L.append(L[x-1] + L[x-2])
return L
f = Fib()
print f(10)
2018-11-23
def calc_prod(lst):
def prod():
def product(x,y):
return x*y
return reduce(product,lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
def product(x,y):
return x*y
return reduce(product,lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-11-22
def read(self):
return r'["Tim", "Bob", "Alice"]'
return r'["Tim", "Bob", "Alice"]'
2018-11-22
class Person(object):
__count = 0
def __init__(self,name):
self.name = name
Person.__count += 1
@classmethod
def how_many(cls):
return cls.__count
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
__count = 0
def __init__(self,name):
self.name = name
Person.__count += 1
@classmethod
def how_many(cls):
return cls.__count
print Person.how_many()
p1 = Person('Bob')
print Person.how_many()
2018-11-22
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
s = u'am I an unicode 中文'
print isinstance(s, unicode)
from __future__ import unicode_literals
s = u'am I an unicode 中文'
print isinstance(s, unicode)
2018-11-22