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

时间:2019-06-27 09:15:25

标签: python

我试图以类似网格的格式随机设置五个“ B”,但是有时代码会在已经存在一个的情况下设置一个“ B”。

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

            'query_string' => [
                'query' => 'product_name:' . $request->product_name . '*',
                'default_operator' => 'AND'
            ]

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

['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']

4 个答案:

答案 0 :(得分:3)

在添加元素并增加计数器之前,检查元素中是否已包含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

答案 1 :(得分:0)

为什么不检查它是否已经包含'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

答案 2 :(得分:0)

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)

输出:

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']]


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

答案 3 :(得分:0)

我更喜欢Barmar的方法,但是这是一种不同的思考方式: 仅在“ B”的总数已更改的情况下,才向c添加+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”数量,然后将该数字附加到列表中。此结果列表的总和就是整个网格中B的数量。