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
is_not_empty=lambda s: s and len(s.strip()) > 0
print filter(is_not_empty, ['test', None, '', 'str', ' ', 'END'])
难道这样不是更简化,运行结果是一样的啊,[・_・?][・_・?]
print filter(is_not_empty, ['test', None, '', 'str', ' ', 'END'])
难道这样不是更简化,运行结果是一样的啊,[・_・?][・_・?]
2018-01-15
写了个递归来实现:
for x in range(num):
l.append(self.get_fbnqlist(x))
self.l=l
def get_fbnqlist(self,num):
if num <= 1:
return num
else:
return self.get_fbnqlist(num-1)+self.get_fbnqlist(num-2)
for x in range(num):
l.append(self.get_fbnqlist(x))
self.l=l
def get_fbnqlist(self,num):
if num <= 1:
return num
else:
return self.get_fbnqlist(num-1)+self.get_fbnqlist(num-2)
2018-01-15
def cmp_ignore_case(s1, s2):
return s1.upper() > s2.upper() and 1 or -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
return s1.upper() > s2.upper() and 1 or -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-01-15
r'["Tim","Bob","Alice"]' 这是什么意思啊
2018-01-15