import math
def is_sqr(x):
return x and math.sqrt(x)== int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return x and math.sqrt(x)== int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
2015-10-02
class Person(object):
def __init__(self,name,gender,birth,job):
self.name = name
self.gender = gender
self.birth = birth
self.job = job
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
def __init__(self,name,gender,birth,job):
self.name = name
self.gender = gender
self.birth = birth
self.job = job
xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student')
print xiaoming.name
print xiaoming.job
2015-10-01
'call %s() in %fs' % (f.__name__,(t2-t1)) 这个%s() 和 %fs' 没看懂
2015-09-28
第二种方法,
for k,v in kw.items():
setattr(self,k,v)
_______________这个函数可以实现的功能也是把**kw,字典里面的k,v赋值进来,self还是找到那个p1或者说该题里面的xiaoming
上面的内容都是
def __init__(self,name,gender,birth,**kw):
self.name=name
self.gender=gender
self.birth=birth
for k,v in kw.items():
setattr(self,k,v)
_______________这个函数可以实现的功能也是把**kw,字典里面的k,v赋值进来,self还是找到那个p1或者说该题里面的xiaoming
上面的内容都是
def __init__(self,name,gender,birth,**kw):
self.name=name
self.gender=gender
self.birth=birth
2015-09-26
这个题的关键就是解决**kw,字典的引入,有两个办法
第一个,for k,v in kw.items():
stm='self.%s=\'%s\''%(k,v)
exec(stm)
#exec(),执行字符串或者文件中的python语句,self为自己定义的一个名字,就像上一节讲义里的p1一样,k,v分别是key,value的缩写,原来的内容里提到过这种用法,可以提取dic,字典里面的关键字和内容
第一个,for k,v in kw.items():
stm='self.%s=\'%s\''%(k,v)
exec(stm)
#exec(),执行字符串或者文件中的python语句,self为自己定义的一个名字,就像上一节讲义里的p1一样,k,v分别是key,value的缩写,原来的内容里提到过这种用法,可以提取dic,字典里面的关键字和内容
2015-09-26
def calc_prod(lst):
def prod(a,b):
return a*b
def my_prod():
return reduce(prod,lst)
return my_prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod(a,b):
return a*b
def my_prod():
return reduce(prod,lst)
return my_prod
f = calc_prod([1, 2, 3, 4])
print f()
2015-09-26
def cmp_ignore_case(s1, s2):
if s1.capitalize() > s2.capitalize():
return 1
if s1.capitalize() < s2.capitalize():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.capitalize() > s2.capitalize():
return 1
if s1.capitalize() < s2.capitalize():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2015-09-26
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-09-26
第三种:
L1 = [p1, p2, p3]
L2=sorted(L1,lambda x,y:cmp(x.name,y.name))
print L2[0].name
print L2[1].name
print L2[2].name
L1 = [p1, p2, p3]
L2=sorted(L1,lambda x,y:cmp(x.name,y.name))
print L2[0].name
print L2[1].name
print L2[2].name
2015-09-26
第二种:L2=sorted(L1,key=lambda m:m.name)
print L2[0].name
print L2[1].name
print L2[2].name
print L2[0].name
print L2[1].name
print L2[2].name
2015-09-26
L1 = [p1, p2, p3]
L2=sorted([L1[0].name,L1[1].name,L1[2].name])
print L2[0]
print L2[1]
print L2[2]
L2=sorted([L1[0].name,L1[1].name,L1[2].name])
print L2[0]
print L2[1]
print L2[2]
2015-09-26
import math
def is_sqr(x):
return x and float(math.sqrt(x))-int(math.sqrt(x)) == 0
print filter(is_sqr, range(0, 101))
呵呵
def is_sqr(x):
return x and float(math.sqrt(x))-int(math.sqrt(x)) == 0
print filter(is_sqr, range(0, 101))
呵呵
2015-09-25
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])
2015-09-25