[Tutor] sort problem

Francesco Loffredo fal at libero.it
Wed Sep 8 18:17:13 CEST 2010


On 08/09/2010 17.50, Roelof Wobben 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
>...
>  > > 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.
Are you sure that you really get your old string? I would expect 
something like:
seq = "cba"
seq2 = ["a", "b", "c"]

seq.join(seq2) => "acbabcbac"

that is, all the characters from seq2 separated by copies of seq.

Evert gave you a good advice:

>  > 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/)¶ <#str.join>
>
> 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.

What does join use as a separator between the elements it joins?


> Roelof
Francesco
-------------- next part --------------

Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com 
Versione: 9.0.851 / Database dei virus: 271.1.1/3119 -  Data di rilascio: 09/07/10 08:34:00


More information about the Tutor mailing list