'''标签:map,return,函数,python,reduce,ret,print,高阶 From: https://www.cnblogs.com/mengdie1978/p/16783238.html
def counter(base):
def inc(step=1):
nonlocal base
base+=step
return base
return inc
foo=counter(5)
foo2=counter(5)
print(foo==foo2)
'''
#sorted
# def comp(a, b):
# return a < b
'''
lst=[1,2,5,4,2,3,5,6]
def sort(iterable,key=lambda a,b:a<b):
ret=[]
for x in iterable:
for i,y in enumerate(ret):
if key(x,y):
ret.insert(i,x)
break
else:
ret.append(x)
return ret
# print(sort(lst,key=lambda a,b:a>b))
lst=[1,2,5,4,2,3,5,6]
print(sorted(lst,key=lambda x:2-x))
'''
# ret=lambda x:(x%5,x)
# print('1',list(map(ret,range(500))))
# print('2',dict(map(ret,range(500))))
# print(dict(map(lambda x:(x%5,x),range(500))))
'''
map函数
map函数接收的是两个参数,一个函数,一个序列,其功能是将序列中的值处理再依次返回至列表内。其返回值为
一个迭代器对象--》例如:<map object at 0x00000214EEF40BA8>。其用法如图:
num=[1,3,2,6,8,7,4]
def square(x):
return x**2
def map_test(func,iter):
num_1=[]
for i in iter:
ret=func(i)
num_1.append(ret)
return num_1.__iter__() #将列表转为迭代器对象
print('1',list(map_test(square,num)))
print('2',list(map(square,num)))
print('3',list(map_test(lambda x:x.upper(),'amanda')))
print('4',list(map(lambda x:x.upper(),'bmanda')))
# 1 [1, 9, 4, 36, 64, 49, 16]
# 2 [1, 9, 4, 36, 64, 49, 16]
# 3 ['A', 'M', 'A', 'N', 'D', 'A']
# 4 ['B', 'M', 'A', 'N', 'D', 'A']
'''
'''
filter函数也是接收一个函数和一个序列的高阶函数,其主要功能是过滤。
其返回值也是迭代器对象,例如:<filter object at 0x000002042D25EA90>,其图示如下
names=["Alex","amanda","xiaowu"]
def filter_test(func,iter):
names_1=[]
for i in iter:
if func(i):#传入的func函数其结果必须为bool值。
names_1.append(i)
return names_1
print(filter_test(lambda x:x.islower(),names))
print(list(filter(lambda x:x.islower(),names)))
# ['amanda', 'xiaowu']
# ['amanda', 'xiaowu']
'''
'''
#reduce函数不是内置函数,而是在模块functools中的函数,故需要导入
from functools import reduce
nums=[1,2,3,4,5,6]
#reduce函数的机制
def reduce_test(func,array,ini=None): #ini作为基数
if ini == None:
ret =array.pop(0)
else:
ret=ini
for i in array:
ret=func(ret,i)
return ret
#reduce_test函数,叠乘
print(reduce_test(lambda x,y:x*y,nums,100))
#reduce函数,叠乘
print(reduce(lambda x,y:x*y,nums,100))
# 求累积 362880
from functools import reduce
def multi(x,y):
return x*y
print(reduce(multi,range(1,10)))
# 求累和 5050
from functools import reduce
def add(x,y):
return x+y
print(reduce(add,[i for i in range(1,101)]))
# 将列表[1, 3, 5, 7, 9]变换成整数13579
from functools import reduce
def fn(x, y):
return x*10 + y
print(reduce(fn, [1, 3, 5, 7, 9]))
# 将字符串'13579'变成整数13579(我们当然也可以直接用int()函数)
from functools import reduce
def fn(x, y):
return x * 10 + y
def char2num(s):
digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
return digits[s]
print(reduce(fn, map(char2num, '13579')))
# filter函数:和map()类似,filter()也接收一个函数和一个序列;和map()不同的是,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。
# 在一个list中,删掉偶数,只保留奇数 [1, 5, 9, 15]
def is_odd(n):
return n % 2 == 1
print(list(filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15])))
# 将一个序列中的空字符串删掉['A', 'B', 'C']
def not_empty(s):
return s and s.strip()
list1 = list(filter(not_empty, ['A', '', 'B', None, 'C', ' ']))
print(list1)
'''