<br><br><div class="gmail_quote">On Wed, Sep 8, 2010 at 11:50 AM, Roelof Wobben <span dir="ltr"><<a href="mailto:rwobben@hotmail.com">rwobben@hotmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div>
<br> <br>
> Subject: Re: [Tutor] sort problem<br>> From: <a href="mailto:evert.rol@gmail.com" target="_blank">evert.rol@gmail.com</a><br>> Date: Wed, 8 Sep 2010 17:26:58 +0200<br>> CC: <a href="mailto:tutor@python.org" target="_blank">tutor@python.org</a><br>
> To: <a href="mailto:rwobben@hotmail.com" target="_blank">rwobben@hotmail.com</a><div class="im"><br>> <br>> > I have this :<br>> > <br>> > def sort_sequence(seq):<br>> > """<br>
> > >>> sort_sequence([3, 4, 6, 7, 8, 2])<br>> > [2, 3, 4, 6, 7, 8]<br>> > >>> sort_sequence((3, 4, 6, 7, 8, 2))<br>> > (2, 3, 4, 6, 7, 8)<br>> > >>> sort_sequence("nothappy")<br>
> > 'ahnoppty'<br>> > """<br>> > if type(seq) == type([]):<br>> > seq.sort()<br>> > elif type(seq)== type(()):<br>> > seq = tuple(sorted(seq))<br>> > else:<br>
> > seq2 = list(seq)<br>> > seq2.sort()<br>> > print seq2<br>> > seq.join(seq2)<br>> > return seq<br>> > <br>> > 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>
> <br>> 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>> <br>> How does it work, what does it return, etc. Then fix the corresponding line in your code.<br>
> As a hint: str.join does work quite different than list.sort; I assume you're confusing their syntaxes.<br>> <br>> Good luck,<br>> <br>> Evert<br>> <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'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't do it in place, i.e. it doesn'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'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 = "".join(seq2)</div><div><br></div><div>where "" 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>