# [Tutor] sort problem

Roelof Wobben rwobben at hotmail.com
Wed Sep 8 19:12:05 CEST 2010

```

Date: Wed, 8 Sep 2010 12:38:03 -0400
From: gregbair at gmail.com
To: tutor at python.org
Subject: Re: [Tutor] sort problem

On Wed, Sep 8, 2010 at 11:50 AM, Roelof Wobben <rwobben at hotmail.com> wrote:

> Subject: Re: [Tutor] sort problem
> From: evert.rol at gmail.com
> Date: Wed, 8 Sep 2010 17:26:58 +0200
> CC: tutor at python.org
> To: rwobben at hotmail.com

>
> > I have this :
> >
> > def sort_sequence(seq):
> > """
> > >>> sort_sequence([3, 4, 6, 7, 8, 2])
> > [2, 3, 4, 6, 7, 8]
> > >>> sort_sequence((3, 4, 6, 7, 8, 2))
> > (2, 3, 4, 6, 7, 8)
> > >>> sort_sequence("nothappy")
> > 'ahnoppty'
> > """
> > if type(seq) == type([]):
> > seq.sort()
> > elif type(seq)== type(()):
> > seq = tuple(sorted(seq))
> > else:
> > seq2 = list(seq)
> > seq2.sort()
> > print seq2
> > seq.join(seq2)
> > return seq
> >
> > 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.
>
> Carefully read the documentation for str.join: http://docs.python.org/library/stdtypes.html#str.join
>
> How does it work, what does it return, etc. Then fix the corresponding line in your code.
> As a hint: str.join does work quite different than list.sort; I assume you're confusing their syntaxes.
>
> Good luck,
>
> Evert
>

str.join(iterable)¶
How it works.
It puts all the elements of iterable into one string named str.

So it returns a string.

Str is here seq  and the iterable is the list made by list.sort so seq2

So I don't see the error in that line.

Roelof

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.

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

seq.join(seq2)

You really want

seq = "".join(seq2)

where "" is the separator to join seq2 on (an empty string in this case)

HTH.

--
Greg Bair
gregbair at gmail.com"

Oke,

If I understand it right with join I can put two strings into 1 string.

Roelof

_______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100908/7913c236/attachment.html>
```