【发布时间】:2021-11-25 02:12:17
【问题描述】:
例如,我有 5 个列表,每个列表包含 10 个元素,每个列表都是用随机值模拟掷硬币生成的。
我通过以下方式获得包含 10 个元素的 5 个列表:
result = [0,1] #0 is tail #1 is head
probability = [1/2,1/2]
N = 10
list = []
def list_generator(number): #this number would be 5 in this case
for i in range(number):
n_round = np.array(rnd.choices(result, probability, k=N))
print(n_round)
list_generator(5)
And for example I would get this
[1 1 0 0 0 1 0 1 1 0]
[0 1 0 0 0 1 1 1 0 1]
[1 1 0 0 1 1 1 0 1 1]
[0 0 0 1 0 0 0 1 0 0]
[0 0 1 1 0 0 0 0 1 1]
我怎样才能只对同一列的数字求和,我的意思是,我想得到一个附加值 1+0+1+0+0(第一列)的列表,然后,该列表附加了每轮第二次抛硬币的总和,即 1+1+1+0+0(第二列),以此类推,十次抛硬币 (我需要它在列表中,因为我将使用它来绘制图表)
我考虑过用每个数组创建一个矩阵,只对第 n 列求和,然后将该值附加到列表中,但我不知道该怎么做,我对使用数组了解不多。
【问题讨论】:
-
你试过什么代码?这是一个家庭作业问题吗?
-
我一直在尝试使用嵌套的 for 循环和 np.matrix() 但没有任何效果。不,这不是作业,它只是一个非常特殊的案例,可以帮助我理解 python 如何处理矩阵和数组。
标签: python numpy matrix plot random