<div dir="ltr">2013/9/17 Marco Beri <span dir="ltr"><<a href="mailto:marcoberi@gmail.com" target="_blank">marcoberi@gmail.com</a>></span><br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div class="im"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div><div>>>> a = [2, -4, 27, 44, 13, 0]</div></div><div>>>> len(x for x in a if x > 5)<br>
</div><div><div>Traceback (most recent call last):</div><div> File "<stdin>", line 1, in <module></div></div><div>TypeError: object of type 'generator' has no len()</div><div>>>> len([x for x in a if x > 5])</div>
<div>3</div><div>>>> list_max = max(a)</div><div>>>> len([x for x in a if x == list_max])</div><div>1</div></div><div><div>>>> len([x for x in a if x == max(a)]) # anche cosi' insomma</div>
<div>
1</div></div></div></div></div></blockquote><div><br></div></div><div>Per quel che vale, come performance molto, molto meglio la prima versione:</div><div><br></div><div>>>> import timeit</div><div><div>>>> timeit.timeit("len([1 for x in a if x == max(a)])", "a = [2, -4, 27, 44, 13, 0]")</div>
<div>5.89989202538311</div><div>>>> timeit.timeit("len([1 for x in a if x == max_a])", "a = [2, -4, 27, 44, 13, 0];max_a=max(a)")</div><div>1.4572058739309028</div><div><br></div></div><div>Circa 4 volte pių veloce, come č ovvio visto che fa una volta sola max invece che 5.</div>
</div></div></div></blockquote><div><br></div><div>Si', non ci piove, l'ho solo riportata per completezza, ma non scrivere mai codice cosi'.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">
<div><br></div><div>Oltre al fatto che mi da un certo fastidio usare una funzione su una lista che sto percorrendo.</div><div><br></div></div></div></div></blockquote><div><br></div><div>Concordo.</div><div><br></div><div>
<div>Ciao,</div><div>Giuliano</div><div><br></div></div></div>-- <br>Piergiuliano Bossi<br>Blog: <a href="http://thinkingbox.wordpress.com/" target="_blank">http://thinkingbox.wordpress.com/</a><br>Twitter: <a href="http://twitter.com/thinkingbox" target="_blank">http://twitter.com/thinkingbox</a> (English)<br>
Twitter: <a href="http://twitter.com/scatolapensante" target="_blank">http://twitter.com/scatolapensante</a> (Italiano)<br><div>Google+: <a href="https://plus.google.com/u/0/108187981162465525118" target="_blank">https://plus.google.com/u/0/108187981162465525118</a></div>
</div></div>