[英]Creating a random number block of 0s and 1s
import random
a = [0]
b = [1]
for row in range(1):
for colum in range(5):
random.shuffle(a and b)
print (" ".join( repr(e) for e in a + b[:5]))
Hey guys, 大家好,
So I'm trying to create a 5 x 5 matrix filled with either 1s or 0s, however, having a bit of a hard time trying to achieve it. 因此,我试图创建一个填充为1或0的5 x 5矩阵,但是要实现它有些困难。 I gave it a go using the code above but no use. 我使用上面的代码尝试了一下,但是没有用。 I'm new to python programming so be gentle haha. 我是python编程的新手,所以要轻柔哈哈。
This is the desired outcome: (There needs to be a MINIMUM of at least 10 1's within the matrix. Any idea of how to do that? 这是理想的结果:(矩阵中的最小值必须至少为101。如何实现该目标?
0 1 1 1 1
0 1 1 0 0
0 1 1 0 1
1 1 0 1 1
1 1 1 1 1
Any advice would be much, much appreciated. 任何建议将不胜感激。 Thank you! 谢谢! :) :)
You can start by getting a random number between 10 and 25, and create a list with that many 1's followed by enough 0's to get to 25. 您可以先获取介于10到25之间的随机数,然后创建一个列表,其中包含多个1,后跟0,以达到25。
import random
ones = random.randint(10, 25)
l = [1] * ones + [0] * (25-ones)
[1] * ones
creates a list with ones
1's. [1] * ones
创建了一个列表ones
1的。 [0] * (25-ones)
creates a list with the remaining 0's. [0] * (25-ones)
创建一个列表,剩余的0表示该列表。 These are then concatenated using the +
to produce a list with 25 total items. 然后使用+
将它们连接起来,以产生一个包含25个项目的列表。
Then shuffle this list: 然后随机播放此列表:
random.shuffle(l)
and finally copy the values into the 5x5 matrix: 最后将值复制到5x5矩阵中:
matrix = [l[i:i+5] for i in range(0, 25, 5)]
range(0, 25, 5)
iterates from 0
to 25
by 5
, ie 0
, 5
, 10
, etc. Then l[i:i+5]
takes a slice of 5 elements starting at each of those indexes. range(0, 25, 5)
迭代从0
至25
由5
,即0
, 5
, 10
,等等。然后l[i:i+5]
需要5种元素的开始在每一个这些索引的切片。 The list comprehension combines these all into a 2-dimensional list. 列表理解将这些全部组合成一个二维列表。
You can use numpy.random.randint
您可以使用numpy.random.randint
>>> import numpy as np
>>> np.random.randint(0, 2, (5, 5))
array([[1, 1, 1, 0, 0],
[1, 0, 1, 1, 1],
[0, 1, 1, 0, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0]])
and then discard the solutions with less than 10 ones 然后丢弃少于10个溶液
For counting number of ones (as you only have 0s and 1s) 用于计数1(因为您只有0和1)
>>> data = np.random.randint(0, 2, (5, 5))
>>> data.sum()
13
random.choices
combined with a list comprehension is a quick way to do this. random.choices
与列表random.choices
结合起来是一种快速的方法。 More often than not you will have 10 ones, but you can loop until you're certain: 通常,您会有10个,但可以循环直到确定:
from random import choices
total = 0
while total < 10:
matrix = [choices((0, 1), k=5) for i in range(5)]
total = sum(map(sum, matrix))
print(matrix)
[[1, 1, 1, 0, 0], [[1,1,1,0,0],
[1, 0, 0, 1, 0], [1、0、0、1、0],
[1, 1, 0, 0, 0], [1、1、0、0、0],
[0, 0, 1, 1, 0], [0,0,1,1,0],
[1, 0, 1, 0, 0]] [1、0、1、0、0]]
In fact, my dear friend there is a simple approach for this problem, and you should put all your code after "if name == " main ":". 实际上,亲爱的朋友,有一个解决此问题的简单方法,您应该将所有代码放在“ if name ==“ main ”:“之后。
import random
def log_matrix(matrix):
for r in range(len(matrix)):
print(" ".join([str(i) for i in matrix[r]]))
def sum_matrix(matrix):
return sum(map(sum, matrix))
def gen_matrix(row, col, minimum):
random_matrix = [[random.choice([0, 1]) for _ in range(row)]
for _ in range(col)]
while sum_matrix(random_matrix) < minimum:
random_matrix[random.choice([0, row - 1])][random.choice(
[0, col - 1])] = 1
return random_matrix
if __name__ == "__main__":
random_matrix = gen_matrix(5, 5, 10)
log_matrix(random_matrix)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.