def cmp_ignore_case(s1, s2):
if s1.lower() < s2.lower():
return -1
if s1.lower() > s2.lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower() < s2.lower():
return -1
if s1.lower() > s2.lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-10-04
class Fib(object):
def __init__(self):
pass
def __call__(self,num):
l=[0,1]
n=2
while n<num:
l.append(l[n-2]+l[n-1])
n+=1
return l
f = Fib()
print f(10)
def __init__(self):
pass
def __call__(self,num):
l=[0,1]
n=2
while n<num:
l.append(l[n-2]+l[n-1])
n+=1
return l
f = Fib()
print f(10)
2015-10-04
def cmp_ignore_case(s1, s2):
if s1.lower() > s2.lower():
return -1
if s1.lower() < s2.lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower() > s2.lower():
return -1
if s1.lower() < s2.lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-10-03
import math
def is_sqr(x):
if math.sqrt(x) == int(math.sqrt(x)):
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if math.sqrt(x) == int(math.sqrt(x)):
return x
print filter(is_sqr, range(1, 101))
2015-10-03
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'])
2015-10-03
def format_name(s):
return s[0].upper() + s[1:]
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper() + s[1:]
print map(format_name, ['adam', 'LISA', 'barT'])
2015-10-03
import math
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
2015-10-03
http://www.imooc.com/code/3517 对可变参数不理解的复习下基础课,函数的可变参数,*args其实是个tuple, **kw是个dict
2015-10-03
class Person(object):
def __init__(self,name,age,sex,*args):
self.name = nam
self.age = age
self.sex = sex
self.args = args
xiaoming = Person("xiaoming",12,"male","teacher","police")
print xiaoming.name
print xiaoming.sex
print xiaoming.args
def __init__(self,name,age,sex,*args):
self.name = nam
self.age = age
self.sex = sex
self.args = args
xiaoming = Person("xiaoming",12,"male","teacher","police")
print xiaoming.name
print xiaoming.sex
print xiaoming.args
2015-10-03