l_o = ['adman', 'LISA', 'batT']
l_n = []
for s in l_o:
s_n = s.capitalize()
l_n.append(s_n)
print l_n
l_n = []
for s in l_o:
s_n = s.capitalize()
l_n.append(s_n)
print l_n
2018-05-08
def format_name(s):
m=s[0:1:].upper()
for i in s[1:]:
m=m+i.lower()
return m
print map(format_name, ['adam', 'LISA', 'barT'])
m=s[0:1:].upper()
for i in s[1:]:
m=m+i.lower()
return m
print map(format_name, ['adam', 'LISA', 'barT'])
2018-05-08
def __cmp__(self, s):
if self.score > s.score:
return -1
elif self.score < s.score:
return 1
else:
return cmp(self.name, s.name)
if self.score > s.score:
return -1
elif self.score < s.score:
return 1
else:
return cmp(self.name, s.name)
2018-05-07
class Students(object):
def __init__(self,):
self.name = r'["Tim", "Bob", "Alice"]'
def read(self):
return self.name
看懂了问题就很简单啊
def __init__(self,):
self.name = r'["Tim", "Bob", "Alice"]'
def read(self):
return self.name
看懂了问题就很简单啊
2018-05-07
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-05-07
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)
2018-05-07
class Person(object):
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count + 1
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count + 1
2018-05-06
sorted_ignore_case = functools.partial(sorted, key = lambda x: x[0].lower() + x[1:] )
2018-05-06
def performance(f):
def new_f(*args, **kw):
start = time.time()
r = f(*args, **kw)
end = time.time()
print 'call', f.__name__+'()', 'in', end - start
return r
return new_f
看了答案才知道自己刚才的写法不对,那个时间并不是函数执行一遍的时间
def new_f(*args, **kw):
start = time.time()
r = f(*args, **kw)
end = time.time()
print 'call', f.__name__+'()', 'in', end - start
return r
return new_f
看了答案才知道自己刚才的写法不对,那个时间并不是函数执行一遍的时间
2018-05-06
def performance(f):
start = time.time()
def new_f(*args, **kw):
return f(*args, **kw)
end = time.time()
print 'call', f.__name__+'()', 'in', end - start
return new_f
主要是时间模块的用法不了解,参考别人的代码做出来了
start = time.time()
def new_f(*args, **kw):
return f(*args, **kw)
end = time.time()
print 'call', f.__name__+'()', 'in', end - start
return new_f
主要是时间模块的用法不了解,参考别人的代码做出来了
2018-05-06
import math
def is_sqr(x):
if (math.sqrt(x) - int(math.sqrt(x))) == 0:
return x *x
print filter(is_sqr, range(1, 101))
(math.sqrt(x) - int(math.sqrt(x)) :浮点的开方值 - 取整的开方值
如果相等:说明这个数开方为整数
def is_sqr(x):
if (math.sqrt(x) - int(math.sqrt(x))) == 0:
return x *x
print filter(is_sqr, range(1, 101))
(math.sqrt(x) - int(math.sqrt(x)) :浮点的开方值 - 取整的开方值
如果相等:说明这个数开方为整数
2018-05-06
def format_name(s):
if s and type(s) == str:
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
if s and type(s) == str:
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-05-06