[Tutor] sort problem
Hello, 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. What went wrong ? Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sort problem
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 What went wrong ? Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sort problem
Subject: Re: [Tutor] sort problem From: evert@gmail.com Date: Wed, 8 Sep 2010 17:26:58 +0200 CC: tutor@python.org To: rwob...@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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sort problem
On Wed, Sep 8, 2010 at 11:50 AM, Roelof Wobben rwob...@hotmail.com wrote: Subject: Re: [Tutor] sort problem From: evert@gmail.com Date: Wed, 8 Sep 2010 17:26:58 +0200 CC: tutor@python.org To: rwob...@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*)¶ #12af20c2e150d2eb_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. 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 gregb...@gmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sort problem
Date: Wed, 8 Sep 2010 12:38:03 -0400 From: gregb...@gmail.com To: tutor@python.org Subject: Re: [Tutor] sort problem On Wed, Sep 8, 2010 at 11:50 AM, Roelof Wobben rwob...@hotmail.com wrote: Subject: Re: [Tutor] sort problem From: evert@gmail.com Date: Wed, 8 Sep 2010 17:26:58 +0200 CC: tutor@python.org To: rwob...@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 gregb...@gmail.com Oke, If I understand it right with join I can put two strings into 1 string. Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sort problem
On 9/8/2010 1:12 PM, Roelof Wobben wrote: If I understand it right You don't. What does put two strings into 1 string mean. Provide an example. What does the documentation say about join? What part of that do you not understand? -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sort problem
Roelof Wobben rwob...@hotmail.com wrote 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. str.join(iterable)¶ It puts all the elements of iterable into one string named str. Thats not what the documentation says... So it returns a string. Thats true., When trying to understand how a function works, or debug these kinds of errors use the prompt to experiment. It's the definitive way of seeing what Python will do. For example try: 123.join([5,6,7]) Can you see what Python has done? Use the prompt it is one of the most powerful tools you have. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sort problem
On 08/09/2010 19.12, Roelof Wobben wrote: ... Oke, If I understand it right with join I can put two strings into 1 string. Roelof Not quite. With join you can put together in one string all the elements of a list of strings. While you do so, you can also put another string as a wall between each element of the list. Let's make a little example: separator = Roelof list = [Wobben, Python, Learner] print separator.join(list) ... what you will get? Guess before you try. Francesco 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sort problem
On 08/09/2010 19.12, Francesco Loffredo wrote: ... a little example: separator = Roelof list = [Wobben, Python, Learner] print separator.join(list) ... what you will get? Guess before you try. There's more, I forgot to add: print separator This is important, this method *returns* a *NEW* string, it does not modify the providing string (here separator)! This means you must save the result somewhere, if you want to use it later: together = separator.join(list) Francesco 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sort problem
Date: Wed, 8 Sep 2010 20:10:28 +0200 From: f...@libero.it To: tutor@python.org Subject: Re: [Tutor] sort problem On 08/09/2010 19.12, Francesco Loffredo wrote: ... a little example: separator = Roelof list = [Wobben, Python, Learner] print separator.join(list) ... what you will get? Guess before you try. There's more, I forgot to add: print separator This is important, this method *returns* a *NEW* string, it does not modify the providing string (here separator)! This means you must save the result somewhere, if you want to use it later: together = separator.join(list) Francesco ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Oke, I now see what everyone try to teach me. Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor