海象操作员转换

Warlus Operator conversion

我有一段涉及海象运算符的代码。我正在尝试将其转换为正常的 python 代码。但我不确定它是否正确发生。

# code with warlus
NUM_ELEMS = cpu_count()
NUM_CORES = len(list_of_data)

fair_core_worload = NUM_ELEMS // NUM_CORES
cores_with_1_more = NUM_ELEMS % NUM_CORES

EXTENTS_OF_SUBRANGES = []
bound = 0

for i, extent_size in enumerate(
    [fair_core_worload + 1 for _ in range(cores_with_1_more)]
    + [fair_core_worload for _ in range(NUM_CORES - cores_with_1_more)]
):
    
    EXTENTS_OF_SUBRANGES.append((bound, bound := bound + extent_size))

按照我的理解,应该是用这段代码才对。

for i, extent_size in enumerate(
    [fair_core_worload + 1 for _ in range(cores_with_1_more)]
    + [fair_core_worload for _ in range(NUM_CORES - cores_with_1_more)]
):
    bound = extent_size
    bound_extended = bound + extent_size
    EXTENTS_OF_SUBRANGES.append((bound, bound_extended))

我没有python3.8来测试海象代码。

看文档,应该是

EXTENTS_OF_SUBRANGES.append((bound, bound + extent_size))
bound = bound + extent_size 

bound 在每次迭代中更新 extent_size,因此您需要跟踪之前的 bound 值:

for i, extent_size in enumerate(
    [fair_core_worload + 1 for _ in range(cores_with_1_more)]
    + [fair_core_worload for _ in range(NUM_CORES - cores_with_1_more)]
):
    prev_bound = bound
    bound += extent_size
    EXTENTS_OF_SUBRANGES.append((prev_bound, bound))
EXTENTS_OF_SUBRANGES.append((bound, bound := bound + extent_size))

可以解构为:

temp = bound + extent_size
EXTENTS_OF_SUBRANGES.append((bound, temp))
bound = temp