有没有办法制作没有重复值的矩阵?

发布于 2025-01-09 15:55:16 字数 565 浏览 0 评论 0原文

import random

def main():
values = [
            [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]
        ]
for r in range(5):
    for c in range(5):
        rand = random.randint(1, 25)
        if values[r][c] != rand:
            values[r][c] = rand

        

for i in range(5):
    print("\n")
    for j in range(5):
        print("\t" + str(values[i][j]), end=" ")

main()

此代码应该打印一个 5 x 5 矩阵,范围为 1-25,且不重复数字。由于某种原因,我输入的 if 命令没有随机化它。有人可以帮我解决问题吗?

import random

def main():
values = [
            [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]
        ]
for r in range(5):
    for c in range(5):
        rand = random.randint(1, 25)
        if values[r][c] != rand:
            values[r][c] = rand

        

for i in range(5):
    print("\n")
    for j in range(5):
        print("\t" + str(values[i][j]), end=" ")

main()

This code should've printed a 5 x 5 matrix with a range of 1-25 without repeating the number. For some reason, the if command I did put in did not randomize it. Can someone help me with the solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

听风念你 2025-01-16 15:55:16

试试这个:

from random import shuffle

numbers = list(range(1, 26))
shuffle(numbers)
values = [[numbers[i * 5 + j] for j in range(5)] for i in range(5)]

首先创建一个从 1 到 25 的数字列表。然后随机洗牌。最后你用它创建一个矩阵。

Try this:

from random import shuffle

numbers = list(range(1, 26))
shuffle(numbers)
values = [[numbers[i * 5 + j] for j in range(5)] for i in range(5)]

First you create a list of numbers from 1 to 25. Then you randomly shuffle it. And finally you create a matrix out of it.

我也只是我 2025-01-16 15:55:16

原始代码显示了尝试确保某个值仅使用一次。

if values[r][c] != rand:
            values[r][c] = rand

但是,代码会检查 values[r][c] 处的特定值,即 0 的初始值。因此,rand 被分配给该位置。这不会阻止生成相同的 rand 值并稍后将其分配给另一个位置(即重复的位置)。

import random


def main():
    values = [
        [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]
    ]
    used_numbers = [] # store used numbers.
    for r in range(5):
        for c in range(5):
            rand = random.randint(1, 25)
            while rand in used_numbers: # True if rand has been used
                # rand has been used
                # generate numbers until an unused number is found
                rand = random.randint(1, 25)
            values[r][c] = rand # assign the number to values[r][c]
            used_numbers.append(rand) # add the number to the list of used
            # numbers.

    for i in range(5):
        print("\n")
        for j in range(5):
            print("\t" + str(values[i][j]), end=" ")


main()

测试结果:

    14  16  25  22  19 

    15  10  18  7   2 

    13  8   20  6   5 

    24  17  9   1   12 

    11  23  4   3   21 

The original code shows an attempt to make sure a value is used only once.

if values[r][c] != rand:
            values[r][c] = rand

However, the code checks a specific value at values[r][c] which is the initial value of 0. So, rand is assigned to that location. This does not prevent the same rand value from being generated and assigned later to another location (i.e., a duplicate).

import random


def main():
    values = [
        [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]
    ]
    used_numbers = [] # store used numbers.
    for r in range(5):
        for c in range(5):
            rand = random.randint(1, 25)
            while rand in used_numbers: # True if rand has been used
                # rand has been used
                # generate numbers until an unused number is found
                rand = random.randint(1, 25)
            values[r][c] = rand # assign the number to values[r][c]
            used_numbers.append(rand) # add the number to the list of used
            # numbers.

    for i in range(5):
        print("\n")
        for j in range(5):
            print("\t" + str(values[i][j]), end=" ")


main()

Test results:

    14  16  25  22  19 

    15  10  18  7   2 

    13  8   20  6   5 

    24  17  9   1   12 

    11  23  4   3   21 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文