如何防止在相同索引中分配元素



我正在尝试以格式像格式一样设置五个" b" s,但是有时在已经存在一个时,代码会设置" b"。

我希望结果看起来像这样:

['0', '0', '0', '0', '0', 'B', '0', '0']
['B', '0', 'B', '0', '0', '0', '0', 'B']
['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', 'B', '0']
['0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0']

,但有时我会得到这样的结果:

['0', '0', '0', '0', '0', 'B', '0', '0']
['B', '0', '0', '0', '0', '0', '0', 'B']
['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']

这是我的代码 -

def board():
    grid0 = []
    grid1 = []
    a = 0
    b = 0
    c = 0
    while a < 8:
        grid0.append("0")
        a += 1
    while b < 8:
        grid1.append(grid0.copy())
        b += 1
    while c < 5:
        grid1[random.randint(0,7)][random.randint(0,7)] = "B"
        c += 1
    for row in grid1:
        print(row)

在添加并增加计数器之前检查该元素是否已经具有B

    while c < 5:
        x = random.randint(0,7)
        y = random.randint(0,7)
        if grid1[x][y] != "B":
            grid1[x][y] = "B"
            c += 1

为什么不检查它是否已经包含'b',如果这样做,请重试!

while c < 5:
    r,c = (random.randint(0,7), random.randint(0,7))
    if not grid1[r][c] == "B":
        grid1[r][c] = "B"
        c += 1

我更喜欢Barmar的方法,但这是考虑它的另一种方法:仅当" b" s的总量变化时,仅加入 1。

while c < 5:
    start_count_b = sum([row.count("B") for row in grid1])
    grid1[random.randint(0,7)][random.randint(0,7)] = "B"
    end_count_b = sum([row.count("B") for row in grid1])
    if end_count_b > start_count_b:
        c += 1

列表综合以您的Grid1中的每个列表(行(,计算其中的" B"的数量,并将其附加到列表中。结果列表的总和是您整个网格中的BS数量。

from pprint import pprint
import random
# using a list comprehension to create the grid
grid = [
    ['0'] * 8  # -> append ['0', '0', '0', '0', '0', '0', '0', '0'] to `grid`
    for i in range(8)  # -> 8 times
]
print('GRID:')
pprint(grid)
# generate an index 5 times
for _ in range(5):
    while 1:
        # keep generating an index until an index without 'B' is found
        x, y = random.randint(0, 7), random.randint(0, 7)
        # if an `empty` index is found, assign 'B' to it and move on to the next index
        if grid[x][y] != 'B':
            grid[x][y] = 'B'
            break
print('nGRID:')
pprint(grid)

输出:

网格:[['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','b','0'], ['0','b','0','0','0','0','0','0','0'], ['0','0','0','0','0','0','0','0','0'],, ['b','0','0','0','0','0','0','0','0','0'], ['0','0','b','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','b','0','0','0','0','0','0']]

最新更新