Python:用于循环的长格式数组

时间:2018-06-09 02:19:14

标签: python

在Python中,通过bracketized for循环构建数组的简短形式是:

def get_row(runner):
    row = some_complicated_function()
    return row

my_array = [get_row(runner) for runner in range(10000)]

如果我想避免for循环的压缩形式,那会是什么样子?

1 个答案:

答案 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))