Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33978

RE: How to sort list of String without considering Special characters and with case insensitive

From "Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Subject RE: How to sort list of String without considering Special characters and with case insensitive
Date 2012-11-27 18:21 +0000
References <e6a4709c-ded7-4092-8822-e3ac02b66271@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.319.1354041785.29569.python-list@python.org> (permalink)

Show all headers | View raw


san wrote:
> 
> Please let me know how to sort the list of String in either ascending / descending order without considering
> special characters and case.
> ex: list1=['test1_two','testOne','testTwo','test_one']
> Applying the list.sort /sorted method results in sorted list ['test1_two', 'testOne', 'testTwo', 'test_one']
> but the without considering the special characters and case it should be
> ['testOne','test_one', 'test1_two','testTwo'] OR ['test_one','testOne','testTwo', 'test1_two' ]
> 
> list.sort /sorted method sorts based on the ascii value of the characters but Please let me knwo how do i
> achieve my expected one

You can pass a key function into list.sort() and sorted(). This
allows you to customize the sorting. In the below examples
I use lambda but you can use a non-lambda function (if you need
more complexity).

Case insensitive searches are often done by converting the 
strings being compared into the same case. Here I turned
them all uppercase.

lst = ['test1_two', 'testOne', 'testTwo', 'test_one']
lst.sort(key=lambda x: x.upper())

This will filter non-alphanumeric characters. You may
be able to create and use a translation table instead.

lst.sort( key=lambda x: ''.join( c.upper() for c in x if c 
in string.letters+string.digits ) )


~Ramit



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

How to sort list of String without considering Special characters and with case insensitive san <santosh.ssit@gmail.com> - 2012-11-27 09:31 -0800
  Re: How to sort list of String without considering Special characters and with case insensitive MRAB <python@mrabarnett.plus.com> - 2012-11-27 17:54 +0000
  RE: How to sort list of String without considering Special characters and with case insensitive "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-27 18:21 +0000

csiph-web