lundi 4 octobre 2021

Sum n values of n-lists with the same index in Python

For example, I have 5 lists with 10 elements each one generated with random values simulating a coin toss.

I get my 5 lists with 10 elements in the following way:

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]

How can I sum only the numbers of the same column, I mean, I would like to get a list that appends the value of 1+0+1+0+0 (the first column), then, that list appends the sum of each second coin toss of each round i.e. 1+1+1+0+0 (the second column), and so on with the ten coin tosses (I need it in a list because I will use this to plot a graph)

I have thought about making a matrix with each array and summing only the nth column and append that value in the list but I do not know how to do that, I do not have much knowledge about using arrays.




Aucun commentaire:

Enregistrer un commentaire