我预计以下 2 个代码段基本上是等效的。
return tuple(tuple( False if (i,j) in neighborhood else avail[i][j]
for i in range(len(avail)))
for j in range(len(avail[i])))
(False, False, True, True, True)
(False, False, True, True, True)
(False, False, True, True, True)
(False, False, True, True, True)
(True, True, True, True, True)
ls = [[val for val in row] for row in avail]
for i in range(len(avail)):
for j in range(len(avail[i])):
if (i,j) in neighborhood:
ls[i][j] = False
return ls
[False, False, False, False, True]
[False, False, False, False, True]
[True, True, True, True, True]
[True, True, True, True, True]
[True, True, True, True, True]
带有 for 循环的那个是“正确的”(这就是我想要的)。为什么列表理解版本交换索引?