<br><br><div class="gmail_quote">On Wed, Sep 8, 2010 at 11:50 AM, Roelof Wobben <span dir="ltr">&lt;<a href="mailto:rwobben@hotmail.com">rwobben@hotmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">




<div>
<br> <br>
&gt; Subject: Re: [Tutor] sort problem<br>&gt; From: <a href="mailto:evert.rol@gmail.com" target="_blank">evert.rol@gmail.com</a><br>&gt; Date: Wed, 8 Sep 2010 17:26:58 +0200<br>&gt; CC: <a href="mailto:tutor@python.org" target="_blank">tutor@python.org</a><br>
&gt; To: <a href="mailto:rwobben@hotmail.com" target="_blank">rwobben@hotmail.com</a><div class="im"><br>&gt; <br>&gt; &gt; I have this :<br>&gt; &gt; <br>&gt; &gt; def sort_sequence(seq):<br>&gt; &gt; &quot;&quot;&quot;<br>
&gt; &gt; &gt;&gt;&gt; sort_sequence([3, 4, 6, 7, 8, 2])<br>&gt; &gt; [2, 3, 4, 6, 7, 8]<br>&gt; &gt; &gt;&gt;&gt; sort_sequence((3, 4, 6, 7, 8, 2))<br>&gt; &gt; (2, 3, 4, 6, 7, 8)<br>&gt; &gt; &gt;&gt;&gt; sort_sequence(&quot;nothappy&quot;)<br>
&gt; &gt; &#39;ahnoppty&#39;<br>&gt; &gt; &quot;&quot;&quot;<br>&gt; &gt; if type(seq) == type([]):<br>&gt; &gt; seq.sort()<br>&gt; &gt; elif type(seq)== type(()):<br>&gt; &gt; seq = tuple(sorted(seq))<br>&gt; &gt; else:<br>
&gt; &gt; seq2 = list(seq)<br>&gt; &gt; seq2.sort()<br>&gt; &gt; print seq2<br>&gt; &gt; seq.join(seq2)<br>&gt; &gt; return seq<br>&gt; &gt; <br>&gt; &gt; The problem is that if I want to sort the characters in a string, the list exist of the sorted characters but as soon as I convert them to a string I get the old string.<br>
&gt; <br>&gt; Carefully read the documentation for str.join: <a href="http://docs.python.org/library/stdtypes.html#str.join" target="_blank">http://docs.python.org/library/stdtypes.html#str.join</a><br>&gt; <br>&gt; How does it work, what does it return, etc. Then fix the corresponding line in your code.<br>
&gt; As a hint: str.join does work quite different than list.sort; I assume you&#39;re confusing their syntaxes.<br>&gt; <br>&gt; Good luck,<br>&gt; <br>&gt; Evert<br>&gt; <br><br>
</div><dt><tt>str.</tt><tt>join</tt><big><font size="5">(</font></big><em>iterable</em><big><font size="5">)</font></big><a title="Permalink to this definition" href="#12af20c2e150d2eb_str.join">¶</a></dt>
 <br>
How it works.<br>
It puts all the elements of iterable into one string named str.<br>
 <br>
So it returns a string. <br>
 <br>
Str is here seq  and the iterable is the list made by list.sort so seq2<br>
 <br>
So I don&#39;t see the error in that line.<br>
 <br>
 <br>
Roelof<br>
 <br>                                               </div>
</blockquote></div><div><br></div>The error is that you misunderstand the usage of str.join.  It doesn&#39;t do it in place, i.e. it doesn&#39;t change the actual string, so you have to have a variable to capture the response.<div>
<br></div><div>The biggest thing, though, is that in str.join, str is not the string to store the joined iterator in, it&#39;s the separator for the string.  so, in your case, where you have</div><div><br></div><div>seq.join(seq2)</div>
<div><br></div><div>You really want</div><div><br></div><div>seq = &quot;&quot;.join(seq2)</div><div><br></div><div>where &quot;&quot; is the separator to join seq2 on (an empty string in this case)<br clear="all"><br></div>
<div>HTH.</div><div><br>-- <br>Greg Bair<div><a href="mailto:gregbair@gmail.com" target="_blank">gregbair@gmail.com</a></div><br>
</div>