干净的方法来写这篇文章
嗨,我正在编写Python功能,当我尝试用孔进行测试时,它显示了:R0912:太多的分支(20/12)(Too-Many-Branches)。我应该如何在清洁剂下面编写此代码?
rows = len(board)
cols = len(board[0])
count = 0
copy_board = deepcopy(board)
for i in range(rows):
for j in range(cols):
if check(i - 1, j - 1, rows, cols):
if copy_board[i - 1][j - 1] == 1:
count += 1
if check(i - 1, j, rows, cols):
if copy_board[i - 1][j] == 1:
count += 1
if check(i - 1, j + 1, rows, cols):
if copy_board[i - 1][j + 1] == 1:
count += 1
if check(i, j - 1, rows, cols):
if copy_board[i][j - 1] == 1:
count += 1
if check(i, j + 1, rows, cols):
if copy_board[i][j + 1] == 1:
count += 1
if check(i + 1, j - 1, rows, cols):
if copy_board[i + 1][j - 1] == 1:
count += 1
if check(i + 1, j, rows, cols):
if copy_board[i + 1][j] == 1:
count += 1
if check(i + 1, j + 1, rows, cols):
if copy_board[i + 1][j + 1] == 1:
count += 1
# Apply the rule to each cell
if count < 2 or count > 3:
board[i][j] = 0
elif count == 3:
board[i][j] = 1
# Reset the count for the next cell
count = 0
Hi I'm writing python function and when I try to test it with pylint it shows: R0912: Too many branches (20/12) (too-many-branches). How should I write this code below cleaner?
rows = len(board)
cols = len(board[0])
count = 0
copy_board = deepcopy(board)
for i in range(rows):
for j in range(cols):
if check(i - 1, j - 1, rows, cols):
if copy_board[i - 1][j - 1] == 1:
count += 1
if check(i - 1, j, rows, cols):
if copy_board[i - 1][j] == 1:
count += 1
if check(i - 1, j + 1, rows, cols):
if copy_board[i - 1][j + 1] == 1:
count += 1
if check(i, j - 1, rows, cols):
if copy_board[i][j - 1] == 1:
count += 1
if check(i, j + 1, rows, cols):
if copy_board[i][j + 1] == 1:
count += 1
if check(i + 1, j - 1, rows, cols):
if copy_board[i + 1][j - 1] == 1:
count += 1
if check(i + 1, j, rows, cols):
if copy_board[i + 1][j] == 1:
count += 1
if check(i + 1, j + 1, rows, cols):
if copy_board[i + 1][j + 1] == 1:
count += 1
# Apply the rule to each cell
if count < 2 or count > 3:
board[i][j] = 0
elif count == 3:
board[i][j] = 1
# Reset the count for the next cell
count = 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
列出两个列表:
dx = [-1,-1,-1,0,0,1,1,1,1]
dy = [-1,0,1,-1,1,1,1,0,1]
然后您可以将if零件写为:
Make two lists:
dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
then you can write the if part as: