# 传一个:
def func(i):return i*2
print([i for i in map(func,[1,'2'])]) # [2,'22']
# 传多个
def func2(x,y):return x+y
print([i for i in map(func2,[1,2],[2,3])]) # [3, 5]
图解:
结合图 map()的作用可以理解为:
# 传一个时
seq=[1,'2']
result=[]
def func(x):return x*2
for i in seq:
result.append(func(i))
print(result)
# 传多个时
seq1=[1,2]
seq2=[2,3]
result=[]
def func2(x,y):return x+y
for x,y in zip(seq1,seq2):
result.append(func2(x,y))
print(result)
当多个可迭代对象的长度不一致时,map只会取最短组合;同时每个可迭代对象相应位置参数类型需一致!(除了py支持的"str"*n)
如:
seq1=[1,2]
seq2=[2,3,4]
result=[]
def func2(x,y):return x+y
for x,y in zip(seq1,seq2):
result.append(func2(x,y))
print(result) #[3,5]
map的function参数可以是lambda对象
如:
print([i for i in map(lambda x, y, z: (f'x:{
x}', f'y:{
y}', f'z:{
z}'), [1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2])])
# [('x:1', 'y:1', 'z:1'), ('x:2', 'y:2', 'z:2')]
print([i for i in filter(lambda k: str(k).islower(), ['Java', 'Python', 'js', 'php'])])
# ['js', 'php']
等同于:
_list=['Java','Python','js','php']
result=[]
def is_lower(str_obj):return str(str_obj).islower()
for i in _list:
if is_lower(i):
result.append(i)
print(result)
def _add(x, y):
return x + y
# 指定initial
print(reduce(_add,[1],3)) # 4
print(reduce(_add, [1, 2], 2)) # 5
# 不指定initial
print(reduce(_add, [1, 2])) # 3
print(reduce(_add,[1])) # 1
print(reduce(_add, [1, 2, 3, 4, 5])) # 15
等同于:
def fact(n):
if n == 1:
return 1
return n + fact(n - 1)
print(fact(5)) # 15
借助lambda:
print(reduce(lambda x, y: x + y, range(1, 6))) # 15
结合实际:假设我们要取出字典的key中包含某个关键字的键值对
如:取出下列字典中key值包含ECU的键值对
key = "ECU"
file_dict = {
'value': 'name',
'刷写ECU': 'burn_ecu_version=ecu_name,burn_package_url,(flash_method)',
'BD升级ECU': 'bd_ecu_version=ecu_name,doip_package_url',
'设置证书': 'set_ecu_certs=set_method,ecu_name,(bench_name)', 'x': {
"ECU": "xx"}}
方法一:引入其他变量
result = {
}
for k, v in file_dict.items():
if key in k:
result[k] = v
print(result)
# {'刷写ECU': 'burn_ecu_version=ecu_name,burn_package_url,(flash_method)', 'BD升级ECU': 'bd_ecu_version=ecu_name,doip_package_url'}
方法二:使用推导式
print(dict((k, v) for k, v in file_dict.items() if key in k))
方法三:reduce+map+filter
from functools import reduce
print(reduce(lambda x, y: x.update(y) or x,
[i for i in map(lambda k: {
k: file_dict[k]}, filter(lambda k: key in k, file_dict))]))
细心的同学肯定发现无法过滤出嵌套key。这是弊端
解决方案:递归
class GetResource:
def __init__(self):
self.result = {
}
def get_resource(self, key_str, data):
"""
从dict中获取包含指定key的k,v
:param key_str:
:param data:
:return:
"""
if not isinstance(data, (dict, list, tuple)):
pass
elif isinstance(data, (list, tuple)):
for index in data:
self.get_resource(key_str, index)
elif isinstance(data, dict):
for k, v in data.items():
if isinstance(v, str):
if key_str in k:
self.result[k] = v
else:
self.get_resource(key_str, v)
return self.result
print(GetResource().get_resource(key, file_dict))
# {'刷写ECU': 'burn_ecu_version=ecu_name,burn_package_url,(flash_method)', 'BD升级ECU': 'bd_ecu_version=ecu_name,doip_package_url', 'ECU': 'xx'}