lundi 12 décembre 2016

How to add and display vertical elements of randomly generated list of lists in python?

The following simple code will generate a list of lists of three elements and the total of the elements in each list on the same line.

import random
for i in xrange(3):
    my_randoms1 = [random.randrange(1,6,1) for _ in range (3)]
    print my_randoms1,"\t", sum(my_randoms1)

Now the question is: How could I add the vertical corresponding elements of all lists and display the sums in a new list on top of all lists as [T1,T2,T3]? Thanks

[T1,T2,T3]

[1, 1, 2]   4
[5, 3, 4]   12
[5, 1, 5]   11




Aucun commentaire:

Enregistrer un commentaire