map()----返回一个List列表,接收单个参数map(f(*),List)列表每个参数运用函数
reduce()---返回一个List列表,接收两个参数reduce(f(*,*),List)列表每个参数累加或累剩
filter()---返回一个筛选之后List,接收单个参数filter(f(*),List)
sorted()---自然排序返回List列表,可以接收多个参数,sorted(List,cmp,key)
reduce()---返回一个List列表,接收两个参数reduce(f(*,*),List)列表每个参数累加或累剩
filter()---返回一个筛选之后List,接收单个参数filter(f(*),List)
sorted()---自然排序返回List列表,可以接收多个参数,sorted(List,cmp,key)
2018-10-31
def cmp_ignore_case(s1, s2):
if ord(s1.lower()[0]) > ord(s2.lower()[0]):
return 1
else:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if ord(s1.lower()[0]) > ord(s2.lower()[0]):
return 1
else:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-10-26
python3要这样实现
print (list(sorted(['bob', 'about', 'Zoo', 'Credit'], key = lambda x:x[0].upper())))
print (list(sorted(['bob', 'about', 'Zoo', 'Credit'], key = lambda x:x[0].upper())))
2018-10-26
L = []
for i in range(1,num + 1):
if(len(L) == 0):
L.append(0)
elif(len(L) == 1):
L.append(1)
else:
L.append(L[len(L) -1] + L[len(L) -2])
self.nums = L
for i in range(1,num + 1):
if(len(L) == 0):
L.append(0)
elif(len(L) == 1):
L.append(1)
else:
L.append(L[len(L) -1] + L[len(L) -2])
self.nums = L
2018-10-25
********code*********
def calc_prod(lst):
def ss():
return x*y
print reduce(ss,lst)
return ss
f = calc_prod([1, 2, 3, 4])
print f()
reduce()函数
from functools import reduce
#3.x中reduce方法归到了functools包中,先要引入。
因为没有引入reduce函数,所以先定义reduce函数,然后再返回该函数
def calc_prod(lst):
def ss():
return x*y
print reduce(ss,lst)
return ss
f = calc_prod([1, 2, 3, 4])
print f()
reduce()函数
from functools import reduce
#3.x中reduce方法归到了functools包中,先要引入。
因为没有引入reduce函数,所以先定义reduce函数,然后再返回该函数
2018-10-22
import math
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
2018-10-22
def calc_prod(lst):
def lazy_prod():
s = 1
for i in lst:
s = s * i
return s
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
s = 1
for i in lst:
s = s * i
return s
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-10-22
class Person(object):
__count = 0
def __init__(self, name):
self.name=name
Person.__count+=1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'attributeError'
__count = 0
def __init__(self, name):
self.name=name
Person.__count+=1
print Person.__count
p1 = Person('Bob')
p2 = Person('Alice')
try:
print Person.__count
except AttributeError:
print 'attributeError'
2018-10-21
import math
def is_sqr(x):
a = math.sqrt(x)
b = int(a)
if a-b==0.0:
return x
print filter(is_sqr, range(1,101))
def is_sqr(x):
a = math.sqrt(x)
b = int(a)
if a-b==0.0:
return x
print filter(is_sqr, range(1,101))
2018-10-20