map() 与 nest.map_structure() 的区别及用法

先看一下参数对比
map(func, *iterables)
nest.map_structure(func, *structure, **check_types_dict)
相同:
两者都是对一个可循环结构的元素依次应用函数的过程。
不同的是:
map()返回一个map 对象,
map_structure()返回一个与参数structure有相同arity的structure。
看下面的小例子,可以很清晰的显示我所说的:

def a(x):
    return x**2

map_ob = map(a,[1,2,3])
b = list(map(a,[1,2,3]))
c = nest.map_structure(a,[1,2,3])

print('b',b,'\nc',c)
print('map_ob_type',type(map_ob))
print('b_type',type(b))
print('c_type',type(c))

运行结果如下:
在这里插入图片描述
也就是说,两个函数都是对list[1,2,3]中的每个元素应用函数a(),
map()返回一个map()对象,我们需要转化为list结构。
map_structure(),因为传入的参数是list,所以返回值也是list结构。

注意:运行代码时需要导入nest这个包,
用 from tensorflow.python.util import nest 即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值