473,503 Members | 1,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 867
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
2531
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
1891
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
1512
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
4218
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
2318
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
4023
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
2054
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
1577
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
1511
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...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.