def cmp_ignore_case(s1, s2):
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-01-16
from os import path
from os import listdir
print listdir(r'/data/webroot/conf')
print path.isdir(r'/data/webroot/conf')
print path.isfile(r'/data/webroot/conf/app.conf')
from os import listdir
print listdir(r'/data/webroot/conf')
print path.isdir(r'/data/webroot/conf')
print path.isfile(r'/data/webroot/conf/app.conf')
2018-01-16
已采纳回答 / 慕仔2331887
第6行返回的是lazy_prod函数的结果,不是lazy_prod函数本身。所以第8行f接收的是一个数值而不是函数,到了第9行执行的时候其实是执行的一个数值,所以会报类型错误。
2018-01-16
def cmp_ignore_case(s1, s2):
if s1[0].lower() > s2[0].lower():
return 1
if s1[0].lower() < s2[0].lower():
return -1
if s1[0].lower() > s2[0].lower():
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[0].lower() > s2[0].lower():
return 1
if s1[0].lower() < s2[0].lower():
return -1
if s1[0].lower() > s2[0].lower():
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-01-16
def perf(unit):
def per_dect(f):
def time_cal(*args,**kw):
start=time.clock()
f(*args,**kw)
end=time.clock()-start
if unit=='s':
print 'call '+f.__name__+'()',int(end),'s'
if unit=='ms':
print 'call '+f.__name__+'()',int(round(end)*1000),'ms'
return f(*args,**kw)
return time_cal
return per_dect
def per_dect(f):
def time_cal(*args,**kw):
start=time.clock()
f(*args,**kw)
end=time.clock()-start
if unit=='s':
print 'call '+f.__name__+'()',int(end),'s'
if unit=='ms':
print 'call '+f.__name__+'()',int(round(end)*1000),'ms'
return f(*args,**kw)
return time_cal
return per_dect
2018-01-16
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print f1, f2, f3
为何是对的
fs = []
for i in range(1, 4):
def f():
return i*i
fs.append(f())
return fs
f1, f2, f3 = count()
print f1, f2, f3
为何是对的
2018-01-16
最赞回答 / 黑色礼帽
1.创建函数count() ,返回一个list2.调用count()的过程中有个for循环,for循环的内容是:创建函数f() ;调用函数f():tmp = f();并将结果tmp添加至列表fs:fs.append(tmp);3.count() 返回列表fs,即返回[1,4,9]4.赋值:f1 , f2 , f3 = count() ;即f1 , f2 , f3 = [1,4,9]
2018-01-16
def cmp_ignore_case(s1, s2):
if ord(s1[0].lower()) > ord(s2[0].lower()):
return 1
elif ord(s1[0].lower()) < ord(s2[0].lower()):
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if ord(s1[0].lower()) > ord(s2[0].lower()):
return 1
elif ord(s1[0].lower()) < ord(s2[0].lower()):
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-01-16
import json
class Students(object):
def read(self):
return '["Tim", "Bob", "Alice"]'
s = Students()
print json.load(s)
class Students(object):
def read(self):
return '["Tim", "Bob", "Alice"]'
s = Students()
print json.load(s)
2018-01-16
return (s[:1].upper() + s[1:].lower()).strip() 可以解决 list中有字符串带有空格的问题,比如“ jack”前面有空格
2018-01-15
无论list里面是整型还是布尔型,还是NONE,照样可以
def is_not_empty(s):
return s and len(s.strip()) > 0
def format_name(s):
if isinstance(s, str):
return s[:1].upper() + s[1:].lower()
L=list(map(format_name,['adam',None,'LISA', 'barT',45,"34d",True]))
print(list(filter(is_not_empty,L)))
def is_not_empty(s):
return s and len(s.strip()) > 0
def format_name(s):
if isinstance(s, str):
return s[:1].upper() + s[1:].lower()
L=list(map(format_name,['adam',None,'LISA', 'barT',45,"34d",True]))
print(list(filter(is_not_empty,L)))
2018-01-15
import math
def is_sqr(x):
if math.sqrt(x)%1==0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if math.sqrt(x)%1==0:
return x
print filter(is_sqr, range(1, 101))
2018-01-15