sort list doesnt work, key=str still doesnt work

N

notnorwegian

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?
 
D

Dan Bishop

[',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse',
'clever', 'handsome']>>> x=sorted([',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse', 'clever', 'handsome'], key=str)
[',', ',', 'CHAPTER', 'Emma', 'I', 'I', 'VOLUME', 'Woodhouse',
'clever', 'handsome']



what do i need to do?

x.sort(key=str.upper)
 
D

Dan Upton

[',', ',', '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?

That's correct behavior for what you gave it. Example from
http://www.python.org/doc/2.4.3/whatsnew/node12.html :
[' ', '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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top