我有一段涉及海象操作符的代码。我正试图将其转换为正常的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 := bound + extent_size))
可以解构为:
temp = bound + extent_size
EXTENTS_OF_SUBRANGES.append((bound, temp))
bound = temp
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 + extent_size))
bound = bound + extent_size