简体   繁体   English

如何将 python 矩阵中的 12 个随机 0 更改为 1

[英]How to change 12 random 0 to 1 in python Matrix

My program is suppose to change 12 zeros on random positions to 1 in python 6x6 matrix.我的程序假设在 python 6x6 矩阵中将随机位置的 12 个零更改为 1。 This is my code.这是我的代码。

from random import randint
M = []
i = 0

for i in range(6):
    M.append([])
    for j in range(6):
        M[i].append(0)

while i < 12:
    index = randint(0,5)
    index2 = randint(0,5)
    if (M[index][index2] == 0):
        M[index][index2] = 1
    i+=1

print(M)

So my matrix is going to look like this at the beggining所以我的矩阵在开始时看起来像这样

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

So I randomly chose an array and the element in chosen array.所以我随机选择了一个数组和所选数组中的元素。 The problem is that different number of zeros is changed every time.问题是每次更改不同数量的零。 Not 12 like I want them to change.不像我希望他们改变的 12 岁。 Does anyone know how to solve this problem?有谁知道如何解决这个问题?

The problem is that the two indices index1 and index2 are randomly chosen with replacement, so a pair of indices can appear more than once.问题是两个索引index1index2是随机选择的替换,因此一对索引可以出现不止一次。 This means that one value will be set to 1 two or more times.这意味着一个值将被设置为 1 两次或更多次。

One slightly complicated way to do this is to create a list of all possible indices and choose a random set from this.一种稍微复杂的方法是创建所有可能索引的列表并从中选择一个随机集。

from random import randint, sample
import itertools
M = []
i = 0

for i in range(6):
    M.append([])
    for j in range(6):
        M[i].append(0)

indices = list(itertools.product(range(6), range(6))
random_indicies = sample(indices, 12)
for ind in random_indicies:
    if (M[ind[0]][ind[1]] == 0):
        M[ind[0]][ind[1]] = 1
    i+=1

print(M)

This creates a list indices which contains all pairs of possible indicies.这将创建一个列表indices ,其中包含所有可能的索引对。 sample picks 12 from these without replacement, which is iterated over and used in the matrix. sample从这些中挑选 12 个而不进行替换,这些样本被迭代并在矩阵中使用。

A much slower, but functional way is to just keep looping until 12 indices are set to 1一个慢得多但实用的方法是保持循环直到 12 个索引设置为 1

from random import randint
M = []
i = 0

for i in range(6):
    M.append([])
    for j in range(6):
        M[i].append(0)

while i < 12:
    index = randint(0,5)
    index2 = randint(0,5)
    if (M[index][index2] == 0):
        M[index][index2] = 1
        i+=1

print(M)

Which just skips i+=1 if an element in M isn't changed, so it keeps iterating until 12 elements in M are changed.如果M中的一个元素没有改变,它只会跳过i+=1 ,所以它会一直迭代直到M中的 12 个元素被改变。

You could instead make a flat array of length 36, randomly choose 12 indeces, change values at indeces to 1, and then reshape to 6x6 in the end:您可以改为制作一个长度为 36 的平面数组,随机选择 12 个 indeces,将 indeces 处的值更改为 1,然后最终重塑为 6x6:

import numpy as np

array = np.zeros(36)
indeces = np.random.choice(np.arange(36), 12, replace = False)
array[indeces] = 1
M = np.reshape(array, (6,6))
print(M)

Two issues in the code:代码中有两个问题:

  1. just before the while loop the value of "i" var is "5", from previous for loop就在 while 循环之前,“i”var 的值为“5”,来自上一个 for 循环
  2. you should increment the "i" var in the while loop, only if one value has been changed from 0 to 1只有当一个值从 0 更改为 1 时,才应在 while 循环中增加“i”变量

Here is the code这是代码

from random import randint
M = []
i = 0

for i in range(6):
    M.append([])
    for j in range(6):
        M[i].append(0)

print(M)

i=0
while i < 12:
    index = randint(0,5)
    index2 = randint(0,5)
    if (M[index][index2] == 0):
        M[index][index2] = 1
        i+=1

print(M)

and the output和 output

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

So you have two issues with your code.所以你的代码有两个问题。 First off, you use the variable i in your for loop.首先,您在 for 循环中使用变量i By the time that's finished running, Python has stored the variable and assigned the value 5 to it.到完成运行时,Python 已经存储了变量并为其分配了值 5。 So when you run your while-loop, you're instead starting at 5 rather than zero like how you would like.因此,当您运行您的 while 循环时,您将从 5 开始,而不是像您希望的那样从 0 开始。 A simple fix would be to use a different variable or assign i=0 again.一个简单的解决方法是使用不同的变量或再次分配i=0

The second problem is your if statement and i+=1 .第二个问题是您的if语句和i+=1 This makes your while loop subject to repeating probabilities, with a 1/36 chance of having only 1 zero change!这使您的 while 循环受到重复概率的影响,有 1/36 的机会只有 1 个零变化! Including the i+=1 statement inside the if statement should fix your issue.if语句中包含i+=1语句应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM