带有条件的for循环 - 试图使它成为一个衬里

时间:2018-05-24 06:44:06

标签: python

for row,col in targets:
    if (board[row][col] == -1 and ((col != 0 and board[row][col - 1] != -1) or (col != len(board) - 1 and board[row][col + 1] != -1) or (row != 0 and board[row - 1][col] != -1) or (row != len(board) - 1 and board[row + 1][col] != -1))): return 1000
return min(penalty)

我试图让它成为一个班轮,但正在做:

return [min(penalty), 1000][True in[(board[row][col] == -1 and ((col != 0 and board[row][col - 1] != -1) or (col != len(board) - 1 and board[row][col + 1] != -1) or (row != 0 and board[row - 1][col] != -1) or (row != len(board) - 1 and board[row + 1][col] != -1)) for row,col in targets)]]

但它不是一回事。

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

return 1000 if any(condition(r, c) for r, c in targets) else min(penalty)

在这里,condition()是你内心深处的条件。

但是这将更难以阅读,并且不会比你已经更快。