Python 并行赋值

标签 python python-3.x

试图弄清楚这个并行分配是如何工作的。完整代码可见here . (原始 Github python 文件)。这是我遇到的问题:

def assign_move(square):

    # Parallel Assignment
    target, direction = max(
        ((neighbor, direction) for direction, neighbor in enumerate(game_map.neighbors(square))
                                if neighbor.owner != myID),
                                default = (None, None),
                                key = lambda t: t[0].production)

我会尽可能多地分解它,但我可能做错了什么。

target, direction = max(iterable, default, key)

我们是否将目标和方向分配给同一件事?我认为并行赋值类似于 x, y = 5, 6

现在,如果我们看一下 iterable,它是这样的:

iterable = ((neighbor, direction) for direction, neighbor in enumerate(game_map.neighbors(square)) if neighbor.owner != myID)

我们如何在 for 循环之前有 (neighbor , direction) ?这里的 if 语句有什么作用?如果 neighbor.owner != myID,我们是否不需要 if block 下面的任何东西来运行?

如果迭代器为空,我们将返回 None, None 如下所示?

default = (None, None),

这就是我们用来确定最大值的函数?

key = lambda t: t[0].production)

背景(如果需要): 这是要在游戏中玩的机器人的代码 Halite .它是在此 GitHub repo 中找到的预建机器人.

最佳答案

此处的max 函数返回(direction , neighbors) 相对于neighbor.production 的最大tuple >(正如我们从 key 中理解的那样)如果 neighbor.owner != myID 发生并且迭代器为空 default,则允许邻居参与此迭代= (None, None) 将是 targetdirections 值,所以 targetneighbor大多数 production 都在最后。

关于Python 并行赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41705136/

相关文章:

python - 使用 QFileSystemModel 扩展 QTreeView 中的项目

python - 如何找到跨越垂直线的图案?

python - OSError : libusb-1. 0.so:无法打开共享对象文件:在 readthedocs 中构建文档时没有此类文件或目录

python-3.x - 无法在cygwin上安装uwsgi

python - 使用 Python 正则表达式验证名称

python - 在嵌套列表中搜索元素并将其打印出来

python - 这两个实现有什么区别?

python - 我需要使用递归函数来检查列表是否被镜像(相同的正向和反向)

python - "Unicode Error "unicodeescape "codec can' t 解码字节...无法在 Python 3 中打开文本文件

python - numpy.loadtxt 分析结果的差异