443,660 Members | 1,105 Online
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

# sort list doesnt work, key=str still doesnt work

 P: n/a >>x [',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse', 'clever', 'handsome'] >>x=sorted([',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse', 'clever', 'handsome'], key=str)x [',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse', 'clever', 'handsome'] >>> what do i need to do? Jun 27 '08 #1
4 Replies

 P: n/a On May 26, 9:46*pm, notnorweg...@yahoo.se wrote: >x [',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse', 'clever', 'handsome']>>x=sorted([',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse', 'clever', 'handsome'], key=str) >x [',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse', 'clever', 'handsome'] what do i need to do? x.sort(key=str.upper) Jun 27 '08 #2

 P: n/a On Mon, May 26, 2008 at 10:46 PM, >>x [',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse', 'clever', 'handsome'] >>>x=sorted([',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse', 'clever', 'handsome'], key=str)x [',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse', 'clever', 'handsome'] >>>> what do i need to do? -- http://mail.python.org/mailman/listinfo/python-list That's correct behavior for what you gave it. Example from http://www.python.org/doc/2.4.3/whatsnew/node12.html : >>sorted('Monty Python') # any iterable may be an input [' ', 'M', 'P', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y', 'y'] It looks like it's sorting based on ASCII value which means all upper case letters come before any lower case letter. Jun 27 '08 #3

 P: n/a On May 27, 12:46 pm, notnorweg...@yahoo.se wrote: what do i need to do? Ideally, you need to read some introductory material and stop offloading whatever the hell it is you're doing to this group. Jun 27 '08 #4

 P: n/a Dan Bishop >x=sorted([',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse', 'clever', 'handsome'], key=str) >>x [',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse','clever', 'handsome'] what do i need to do? x.sort(key=str.upper) Or another option to consider: >>import localelocale.setlocale(locale.LC_COLLATE, 'en') 'English_United States.1252' >>print ', '.join(sorted(['CHAPTER', 'eu', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse', 'clever', 'handsome', '\xe9t\xe9'], key=locale.strxfrm)) CHAPTER, clever, Emma, été, eu, handsome, I, I, VOLUME, Woodhouse Annoyingly strxfrm in Python 2.5 can't cope with unicode so if you go this way you need to make sure that the strings are all encoded with the correct encoding (which in this example is the 1252 at the end of the string returned from setlocale) or define a new key function which applies the correct encoding. -- Duncan Booth http://kupuguy.blogspot.com Jun 27 '08 #5

### This discussion thread is closed

Replies have been disabled for this discussion.