<br><div class="gmail_quote">On Thu, Sep 24, 2009 at 8:54 PM, Evora <span dir="ltr"><<a href="mailto:lasse_lorentzen@hotmail.com">lasse_lorentzen@hotmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
Hello,<br>
<br>
I'm a bit of a newbie to python, so this may very well be a basic question:<br>
<br>
I have a list of lists, with around 1000 lists looking like this:<br>
['0.000744', '0.480106', 'B'].<br>
<br>
I need the average of the first to entries of all the lists, can anybody<br>
help how me to do this?<br></blockquote><div><br>Say if we have:<br><br><div style="margin-left: 40px;">data = [['1', '2', 'B'],<br>        ['2', '3', 'B'],<br>        ['3', '4', 'B']]<br>
<br></div>The easier way is to iterate through the big list, and get out the first item and add them. Once you've added them all, divide by the number of lists in the big list.<br><div style="margin-left: 40px;"><br></div>
<div style="margin-left: 40px;">total = 0<br>for list in data:<br>    total += int(list[0])<br>print(total/len(data))<br></div><br># OUTPUT<br><div style="margin-left: 40px;">2.0<br><br></div>Or there is some ... not so obvious way, but same idea:<br>
<br><div style="margin-left: 40px;">print(sum(int(numbers[0]) for numbers in data) / len(data))<br></div><br>
# OUTPUT<br>
<div style="margin-left: 40px;">2.0<br><br></div>(I'm using Python 3.1 by the way. You might have to change the print function.)<br><br>Cheers,<br>Xav<br> </div></div>