<br><br><div class="gmail_quote">2009/1/19 John Fouhy <span dir="ltr">&lt;<a href="mailto:john@fouhy.net">john@fouhy.net</a>&gt;</span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
2009/1/20 Emad Nawfal (عماد نوفل) &lt;<a href="mailto:emadnawfal@gmail.com">emadnawfal@gmail.com</a>&gt;:<br>
<div class="Ih2E3d">&gt; Hello tutors,<br>
&gt; I need to find the shortest / longest word(s) in a sequence of words. I&#39;ve<br>
&gt; done this and it works, but I&#39;m wondering whether this is a good way:<br>
&gt;&gt;&gt;&gt; words = &quot;man woman children he&quot;.split()<br>
&gt;&gt;&gt;&gt; words<br>
&gt; [&#39;man&#39;, &#39;woman&#39;, &#39;children&#39;, &#39;he&#39;]<br>
&gt;&gt;&gt;&gt; lens = [len(word) for word in words]<br>
&gt;&gt;&gt;&gt; lens<br>
&gt; [3, 5, 8, 2]<br>
&gt;&gt;&gt;&gt; for word in words:<br>
&gt; ... &nbsp; &nbsp; if len(word) == min(lens): print word<br>
&gt; ...<br>
&gt; he<br>
<br>
</div>Hi Emad,<br>
<br>
You can use the decorate-sort-undecorate idiom to make this technique<br>
a bit nicer.<br>
<br>
&quot;decorate&quot; means &quot;add information to the things in the list&quot;:<br>
<br>
&gt;&gt;&gt; words_and_lengths = [(len(w), w) for w in words]<br>
&gt;&gt;&gt; words_and_lengths<br>
[(3, &#39;man&#39;), (5, &#39;woman&#39;), (8, &#39;children&#39;), (2, &#39;he&#39;)]<br>
<br>
Now I can sort it and get the shortest element easily:<br>
<br>
&gt;&gt;&gt; words_and_lengths.sort()<br>
&gt;&gt;&gt; words_and_lengths[0]<br>
(2, &#39;he&#39;)<br>
<br>
Or I can undecorate:<br>
<br>
&gt;&gt;&gt; words2 = [w[1] for w in words_and_lengths]<br>
&gt;&gt;&gt; words2<br>
[&#39;he&#39;, &#39;man&#39;, &#39;woman&#39;, &#39;children&#39;]<br>
<br>
Python 2.5+ provides another way of doing this, using the key=<br>
argument to sort():<br>
<br>
&gt;&gt;&gt; words<br>
[&#39;man&#39;, &#39;woman&#39;, &#39;children&#39;, &#39;he&#39;]<br>
&gt;&gt;&gt; words.sort(key=len)<br>
&gt;&gt;&gt; words<br>
[&#39;he&#39;, &#39;man&#39;, &#39;woman&#39;, &#39;children&#39;]<br>
<br>
This essentially does the decorate-sort-undecorate in one step, where<br>
len is the function we used to do the decoration.<br>
<br>
Of course, this is not necessarily the best answer for your particular<br>
problem. &nbsp;The problem with sorting is that you have to look at some<br>
elements more than once. &nbsp;For short lists, it&#39;s not a problem, but it<br>
can slow you down on bigger lists. &nbsp;You could also find the shortest<br>
element by going through the list, remembering the shortest element<br>
you&#39;ve seen so far. &nbsp;This will be quicker if you only want to find the<br>
single shortest.<br>
<br>
--<br>
<font color="#888888">John.<br>
</font></blockquote></div><br>Thanks John for this. Although the decorate-sort-undecorate idiom looks so natural to me now, I don&#39;t think I would have found it on my own. I have that deja vu effect towards it.<br>Thanks again.<br clear="all">
<br>-- <br>لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد الغزالي<br>&quot;No victim has ever been more repressed and alienated than the truth&quot;<br><br>Emad Soliman Nawfal<br>Indiana University, Bloomington<br>
<a href="http://emnawfal.googlepages.com">http://emnawfal.googlepages.com</a><br>--------------------------------------------------------<br>