最新回答 / 飞雪随风
这个是Python参考文档的一部分sorted(iterable[,
cmp[, key[, reverse]]])Return a new sorted list from the items in iterable.The optional arguments cmp, key, and reverse have
the same meaning as those for the list.sort() method (described in section Mutable
Sequence ...
2016-02-19
def is_sqr(x):
return math.floor(math.sqrt(x))==math.ceil(math.sqrt(x))
return math.floor(math.sqrt(x))==math.ceil(math.sqrt(x))
2016-02-18
print (list(filter(lambda x:x and len(x.strip())>0, ['test', None, '', 'str', ' ', 'END'])))
3.4版本的和老版本还是差别挺大的
3.4版本的和老版本还是差别挺大的
2016-02-17
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'
2016-02-16
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])
2016-02-16
def format_name(s):
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.capitalize()
print map(format_name, ['adam', 'LISA', 'barT'])
2016-02-16
def performance(f):
def fn(*args, **kw):
t = time.strftime('%Y-%m-%d %H:%M:%S')
print 'call' + f.__name__ + '()in...' + t
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 fn(*args, **kw):
t = time.strftime('%Y-%m-%d %H:%M:%S')
print 'call' + f.__name__ + '()in...' + t
return f(*args, **kw)
return fn
@performance
def factorial(n):
return reduce(lambda x, y: x * y, range(1, n + 1))
print factorial(10)
2016-02-16
def calc_prod(lst):
def prod():
s=1
for i in lst:
s=s*i
return s
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
s=1
for i in lst:
s=s*i
return s
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-02-16
def cmp_ignore_case(s1, s2):
if s1[0].upper()<s2[0].upper():
return -1
elif s1[0].upper()>s2[0].upper():
return 1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[0].upper()<s2[0].upper():
return -1
elif s1[0].upper()>s2[0].upper():
return 1
else:
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-02-15
import math
def is_sqr(x):
sq = math.sqrt(x)
if sq % 1 == 0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
sq = math.sqrt(x)
if sq % 1 == 0:
return x
print filter(is_sqr, range(1, 101))
2016-02-15
最新回答 / 再见你
<...code...>试着整理一下吧:如果<...code...>也就是说a和fn()这个函数是等价的下面<...code...>也就意味着b和f(x)是等价的f(x)是一个有着确认结果的值的,也就是说,b是一个list、int、float、str等的一个值,不再是一个函数了。
2016-02-15
import time
def performance(unit):
def performance_decorator(f):
def wrapper(*args,**kw):
print 'call'+f.__name__+'()...'
return f(*args,**kw)
return wrapper
return performance_decorator
def performance(unit):
def performance_decorator(f):
def wrapper(*args,**kw):
print 'call'+f.__name__+'()...'
return f(*args,**kw)
return wrapper
return performance_decorator
2016-02-12