Haskell有一个很棒的函数,mapKeysWith。在地图的关键点上,它应用一些变换,将数据对象与给定函数结合起来,以防现在发生冲突。我创建了下面的示例代码,非常详细。还有更像Python的方式吗
def mapKeysWith(combineF,op,mp):
ret = {}
for it in mp:
if op(it) in ret:
ret[op(it)] = combineF(ret[op(it)],mp[it])
else:
ret[op(it)] = mp[it]
return ret
z = {1:2,3:4,10:11}
mapKeysWith(lambda x,y: x+y,lambda x: math.floor(x/10),z)
以下是两种可能的替代方案:
输出
相关问题 更多 >
编程相关推荐