gpt4 book ai didi

list - 如何将列表索引(网格)与片段元素进行比较?

转载 作者:行者123 更新时间:2023-12-04 03:49:51 25 4
gpt4 key购买 nike

grid = []
piece = ( (0, 0), (0, 1), (0, 2), (0, 3) )

for f in range(18):
grilla_vacia.append([])
for c in range(9):
if [f][c] in piece:
grilla_vacia[f].append(1)
grilla_vacia[f].append(0)

我该怎么做,才能在网格中按 block “忙”的位置添加“1”?注释行抛出了这个错误:'list index out of range'

最佳答案

这对您有帮助:

import numpy as np

piece = ( (0, 0), (0, 1), (0, 2), (0, 3) )
grid = np.zeros((9,18),dtype=int).tolist()

for p in piece:
grid[p[1]][p[0]] = 1

print(grid)

输出:

[[1, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]

这是一个没有 numpy 的解决方案:

piece = ( (0, 0), (0, 1), (0, 2), (0, 3) )

grid = [0 for x in range(9*18)]
grid = [grid[i:i+9] for i in range(0, len(grid), 9)]

for p in piece:
grid[p[1]][p[0]] = 1

print(grid)

输出:

[[1, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0]]

关于list - 如何将列表索引(网格)与片段元素进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64590631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com