有更好的python等价物吗?

2024-10-02 08:19:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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)

Tags: 数据对象lambda函数inhaskell地图it
1条回答
网友
1楼 · 发布于 2024-10-02 08:19:58

以下是两种可能的替代方案:

import math
from functools import reduce
from collections import defaultdict


def map_keys_with(combine_f, op, mp):
    ret = {}
    for it in mp:

        if op(it) in ret:
            ret[op(it)] = combine_f(ret[op(it)], mp[it])
        else:
            ret[op(it)] = mp[it]
    return ret


def map_keys_with_setdefault(combine_f, op, mp):
    ret = {}
    for key, value in mp.items():
        ret.setdefault(op(key), []).append(value)

    return {key: reduce(combine_f, values) for key, values in ret.items()}


def map_keys_with_defaultdict(combine_f, op, mp):
    ret = defaultdict(list)
    for key, value in mp.items():
        ret[op(key)].append(value)
    return {key: reduce(combine_f, values) for key, values in ret.items()}


z = {1: 2, 3: 4, 10: 11}
print(map_keys_with(lambda x, y: x + y, lambda x: math.floor(x / 10), z))
print(map_keys_with_setdefault(lambda x, y: x + y, lambda x: math.floor(x / 10), z))
print(map_keys_with_defaultdict(lambda x, y: x + y, lambda x: math.floor(x / 10), z))

输出

{0: 6, 1: 11}
{0: 6, 1: 11}
{0: 6, 1: 11}

相关问题 更多 >

    热门问题