def cmp_ignore_case(s1,s2):
x = s1.lower()
y = s2.lower()
if ord(x[0])>ord(y[0]):
return 1
if ord(x[0])<ord(y[0]):
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
x = s1.lower()
y = s2.lower()
if ord(x[0])>ord(y[0]):
return 1
if ord(x[0])<ord(y[0]):
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-01-20
最赞回答 / 写忆3789911
运行f1, f2, f3 = count() 得到的是三个没有运行的函数,直接print(f1, f2, f3)得到的是三个函数的地址。由于运行count()返回了三个函数(循环了三次),当然需要三个变量来接收(f1, f2, f3 = count() )。当然也可以这样l = count() ,打印方式 print(a[0](), a[1](), a[2]()),这也是可以的。详细代码如下:def count(): fs = [] for i in range(1, 4): ...
2018-01-20
def calc_prod(lst):
def lazy_calc_prod():
k = 1;
for i in lst:
k=k*i
return k
return lazy_calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_calc_prod():
k = 1;
for i in lst:
k=k*i
return k
return lazy_calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-01-20
import math
def is_sqr(x):
return math.sqrt(x) and math.sqrt(x) == math.floor(math.sqrt(x))
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x) and math.sqrt(x) == math.floor(math.sqrt(x))
print filter(is_sqr, range(1, 101))
2018-01-20
import math
def is_sqr(x):
return math.sqrt(x)%1 == 0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)%1 == 0
print filter(is_sqr, range(1, 101))
2018-01-19
from functools import reduce
def prod(x, y):
return x*y
print (reduce(prod, [2, 4, 5, 7, 12]))
def prod(x, y):
return x*y
print (reduce(prod, [2, 4, 5, 7, 12]))
2018-01-19
def format_name(s):
#return s[:1].upper()+s[1:].lower() #方法一
#return s.capitalize()#方法二
return s.title() #方法三
print (list(map(format_name, ['adam', 'LISA', 'barT'])))
#return s[:1].upper()+s[1:].lower() #方法一
#return s.capitalize()#方法二
return s.title() #方法三
print (list(map(format_name, ['adam', 'LISA', 'barT'])))
2018-01-19
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'])
2018-01-19
class Person(object):
def __init__(self, name, gender, **kw):
self.name=name
self.gender=gender
for k in kw:
setattr(self,k,kw[k])
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
def __init__(self, name, gender, **kw):
self.name=name
self.gender=gender
for k in kw:
setattr(self,k,kw[k])
p = Person('Bob', 'Male', age=18, course='Python')
print p.age
print p.course
2018-01-19
print filter(lambda s:s and len (s.strip())>0, ['test', None, '', 'str', ' ', 'END'])
2018-01-19
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-01-19
def calc_prod(lst):
def ss:
sum=1
for i in range(1,len(lst)):
sum=sum*lst[i]
return sum;
return ss
f = calc_prod([1, 2, 3, 4])
print f()
我故意写错了很多地方 大家分析下
def ss:
sum=1
for i in range(1,len(lst)):
sum=sum*lst[i]
return sum;
return ss
f = calc_prod([1, 2, 3, 4])
print f()
我故意写错了很多地方 大家分析下
2018-01-18