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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ramit.prasad@jpmorgan.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'case.': 0.05; 'ascii': 0.07; 'completeness': 0.07; 'subject:How': 0.09; 'ex:': 0.09; 'lst': 0.09; 'received:155': 0.09; 'skip:[ 30': 0.09; 'subject:characters': 0.09; 'ascending': 0.16; 'descending': 0.16; 'disclaimers': 0.16; 'disclaimers,': 0.16; 'from:addr:jpmorgan.com': 0.16; 'insensitive': 0.16; 'lambda': 0.16; 'received:155.180': 0.16; 'received:159.53': 0.16; 'received:169.70': 0.16; 'received:exchad.jpmchase.net': 0.16; 'received:jpmchase.com': 0.16; 'received:jpmchase.net': 0.16; 'securities,': 0.16; 'sorting.': 0.16; 'sorts': 0.16; 'subject: \n ': 0.16; 'subject:String': 0.16; 'subject:case': 0.16; 'subject:insensitive': 0.16; 'url:disclosures': 0.16; 'url:jpmorgan': 0.16; 'translation': 0.16; 'string': 0.17; 'wrote:': 0.17; 'examples': 0.18; 'to:name:python- list@python.org': 0.20; 'sort': 0.21; 'to:2**1': 0.23; 'received:169.254': 0.24; 'allows': 0.25; 'pass': 0.25; 'header :In-Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'accuracy': 0.27; 'converting': 0.27; 'instead.': 0.27; 'subject:list': 0.28; 'searches': 0.29; 'received:169': 0.29; 'function': 0.30; 'header:Received:8': 0.30; "skip:' 20": 0.32; 'to:addr:python- list': 0.33; 'done': 0.34; 'list': 0.35; 'compared': 0.35; 'filter': 0.35; 'expected': 0.35; 'table': 0.35; 'but': 0.36; 'characters': 0.36; 'method': 0.36; 'subject:with': 0.36; 'should': 0.36; 'charset:us-ascii': 0.36; 'being': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'skip:n 10': 0.63; 'information,': 0.63; 'url:email': 0.63; 'more': 0.63; 'here': 0.65; 'legal': 0.65; 'results': 0.65; 'subject': 0.66; 'purchase': 0.67; 'applying': 0.69; 'special': 0.73; 'sale': 0.76; 'received:169.254.8': 0.84; 'subject:\t': 0.91; 'subject:Special': 0.93
X-DKIM OpenDKIM Filter v2.1.3 sf1.jpmchase.com qARILA72032731
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpmorgan.com; s=smtpout; t=1354040470; bh=0GVx+kz4n7Xfnnv18RxiC454E3zxt5qOAVcDANK4yKE=; h=From:To:Subject:Date:Message-ID:References:In-Reply-To: Content-Transfer-Encoding:MIME-Version:Content-Type; b=P4IxIyfiogPEZj7d24yIud9pTZ86UhigKvx6Iygiw8UtzeBq8JWlxUyrthy0n3/3C obdL7arYR2w+mlmOGK31Bm5raGKiaeIV9UoKsgGjWRoW0rTpAePdoT62jrtYSQJgSs vWVFvXkXMqONotL//jHeT6F1RcyA/tvNJPhBCmaM=
From "Prasad, Ramit" <ramit.prasad@jpmorgan.com>
To san <santosh.ssit@gmail.com>, "python-list@python.org" <python-list@python.org>
Subject RE: How to sort list of String without considering Special characters and with case insensitive
Thread-Topic How to sort list of String without considering Special characters and with case insensitive
Thread-Index AQHNzMWQzdunZ61NR06rOV9F9gclfpf9+Q6g
Date Tue, 27 Nov 2012 18:21:03 +0000
References <e6a4709c-ded7-4092-8822-e3ac02b66271@googlegroups.com>
In-Reply-To <e6a4709c-ded7-4092-8822-e3ac02b66271@googlegroups.com>
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
x-originating-ip [10.67.79.47]
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
X-DLP-FWD Yes
Content-Type text/plain; charset="us-ascii"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.319.1354041785.29569.python-list@python.org> (permalink)
Lines 25
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1354041785 news.xs4all.nl 6984 [2001:888:2000:d::a6]:42098
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33978

Show key headers only | 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