干净的方法来写这篇文章

发布于 2025-01-28 16:10:55 字数 1372 浏览 2 评论 0原文

嗨,我正在编写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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亽野灬性zι浪 2025-02-04 16:10:55

列出两个列表:
dx = [-1,-1,-1,0,0,1,1,1,1]
dy = [-1,0,1,-1,1,1,1,0,1]

然后您可以将if零件写为:

for k in range(len(dx)):
    if check(i+dx[k], j+dy[k], rows, cols) and copy_board[i+dx[k]][j+dy[k]]:
         count+=1

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:

for k in range(len(dx)):
    if check(i+dx[k], j+dy[k], rows, cols) and copy_board[i+dx[k]][j+dy[k]]:
         count+=1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文