python - Python:长形式的数组 For 循环
问题描述
bracketized for
在 Python 中,通过循环构建数组的简写形式是:
def get_row(runner):
row = some_complicated_function()
return row
my_array = [get_row(runner) for runner in range(10000)]
如果我想避免for
循环的压缩形式,这会是什么样子?
解决方案
以下代码片段(您提供的是列表理解)
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))
推荐阅读
- mysql - MySQL 中的聚合函数 GROUP_CONCAT
- c# - Renci SshNet“服务器响应不包含ssh协议标识”
- visual-studio - 如何将 Azure Analysis Services 项目从只读更改为读写?
- r - 我不知道如何使用 rgl 向 3d 图形添加标题
- javascript - OnClick 钩子调用 - 反应
- python - 如何使用python客户端和C服务器通过套接字正确发送图像
- c# - 表单中的 C# 组合框不添加项目
- r - 对数的底数大于 10 的 ggplot scale_x_log10()
- c++ - 如何将两个从十进制转换的二进制数相加
- javascript - 用户上传音频以仅在客户端 javascript 上播放