I have the following list of tuples:
list = [(120, 'x'), (1120, 'y'), (1330, 'x'), (0, 't'), (1, 'x'), (0, 'd'), (2435, 'x')]
I would like to calculate the mean of the first component of all tuples. I did the following:
s = []
for i in range(len(list)):
a = list[0][i]
if a =! 0:
s.append(a)
else:
pass
mean = sum(s) / len(s)
and it works, but my question is whether there is any way to avoid using for loops? since I have a very large list of tuples and due to time calculation I need to find another way if that possible.
According to the above stated for loop method. How could I find the mean with regard to the wights? I mean, e.g. the last element in the list is (2435, 'x')
and the number 2435
is very large in comparison to that one in (1, 'x')
which is 1
. Any ideas would be very appreciated. Thanks in advance.