<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
<BR>&nbsp;<BR>
<HR id=stopSpelling>
Date: Wed, 8 Sep 2010 12:38:03 -0400<BR>From: gregbair@gmail.com<BR>To: tutor@python.org<BR>Subject: Re: [Tutor] sort problem<BR><BR><BR><BR>
<DIV class=ecxgmail_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 style="BORDER-LEFT: #ccc 1px solid; PADDING-LEFT: 1ex" class=ecxgmail_quote>
<DIV><BR>&nbsp;<BR>&gt; Subject: Re: [Tutor] sort problem<BR>&gt; From: <A href="mailto:evert.rol@gmail.com">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">tutor@python.org</A><BR>&gt; To: <A href="mailto:rwobben@hotmail.com">rwobben@hotmail.com</A>
<DIV class=ecxim><BR>&gt; <BR>&gt; &gt; I have this :<BR>&gt; &gt; <BR>&gt; &gt; def sort_sequence(seq):<BR>&gt; &gt; """<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("nothappy")<BR>&gt; &gt; 'ahnoppty'<BR>&gt; &gt; """<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'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="http://sn118w.snt118.mail.live.com/mail/RteFrame.html?v=15.3.2529.0827&amp;pf=pf#12af20c2e150d2eb_str.join">¶</A></DT>&nbsp;<BR>How it works.<BR>It puts all the elements of iterable into one string named str.<BR>&nbsp;<BR>So it returns a string. <BR>&nbsp;<BR>Str is here seq&nbsp; and the iterable is the list made by list.sort so seq2<BR>&nbsp;<BR>So I don't see the error in that line.<BR>&nbsp;<BR>&nbsp;<BR>Roelof<BR>&nbsp;<BR></DIV></BLOCKQUOTE></DIV>
<DIV><BR></DIV>The error is that you misunderstand the usage of str.join. &nbsp;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. &nbsp;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">gregbair@gmail.com</A>"</DIV>
<DIV>&nbsp;</DIV>
<DIV>Oke, </DIV>
<DIV>&nbsp;</DIV>
<DIV>If I understand it right with join I can put two strings into 1 string.</DIV>
<DIV>&nbsp;</DIV>
<DIV>Roelof</DIV>
<DIV>&nbsp;</DIV><BR></DIV><BR>_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor                                               </body>
</html>