在Python中,通过bracketized for
循环构建数组的简短形式是:
def get_row(runner):
row = some_complicated_function()
return row
my_array = [get_row(runner) for runner in range(10000)]
如果我想避免for
循环的压缩形式,那会是什么样子?
答案 0 :(得分:1)
以下代码段(您提供的代码段是列表理解)
my_array = [get_row(runner) for runner in range(10000)]
以下是等效的unrolled
版本
my_array = []
for runner in range(10000):
row_runner = get_row(runner)
my_array.append(row_runner)
# alternatively
# my_array.append(get_row(runner))