473,287 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

sort list doesnt work, key=str still doesnt work

>>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 864
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
On Mon, May 26, 2008 at 10:46 PM, <no**********@yahoo.sewrote:
>>>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
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
Dan Bishop <da*****@yahoo.comwrote:
On May 26, 9:46*pm, notnorweg...@yahoo.se wrote:
>>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 locale
locale.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences,...
2
by: Helge | last post by:
I wonder how this can be accomplished: I've got a list, containing several strings. The strings look like this: I would like to create one object for each item in the list, and the name...
11
by: George Sakkis | last post by:
Hi all, I find the string representation behaviour of builtin containers (tuples,lists,dicts) unintuitive in that they don't call recursively str() on their contents (e.g. as in Java) : ...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
6
by: alexhong2001 | last post by:
Does "std::sort" work only with sequence containers, not associative containers at all? Among sequential containers, can it be used with "list", "queue" and other sequence containers besides...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
2
by: joesindel | last post by:
So heres the deal... I work for a company in which numbers are dropping. The owners created a game where calling potential clients racks up points for crap like basketball ticekts and such. The...
13
by: 7stud | last post by:
I can't get the str() method to work in the following code(the last line produces an error): ============ class test: """class test""" def __init__(self): """I am init func!""" self.num = 10...
2
by: Jason | last post by:
Hi folks-- Basically, I have a pressing need for a combination of 5.2 "Sorting a List of Strings Case-Insensitively" & 5.3 "Sorting a List of Objects by an Attribute of the Objects" from the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.