Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33974
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python@mrabarnett.plus.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; 'output': 0.04; 'case.': 0.05; 'ascii': 0.07; 'subject:How': 0.09; 'python': 0.09; 'accepts': 0.09; 'argument,': 0.09; 'ex:': 0.09; 'skip:[ 30': 0.09; 'special,': 0.09; 'subject:characters': 0.09; 'def': 0.10; "''.join(c": 0.16; "(i'm": 0.16; 'ascending': 0.16; 'comparison:': 0.16; 'descending': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'list1': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'sorts': 0.16; 'subject: \n ': 0.16; 'subject:String': 0.16; 'subject:case': 0.16; 'subject:insensitive': 0.16; 'string': 0.17; 'wrote:': 0.17; '>>>': 0.18; 'define': 0.20; 'sort': 0.21; 'import': 0.21; 'lets': 0.22; 'defined': 0.22; 'sets': 0.23; 'pass': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:[ 10': 0.26; 'subject:list': 0.28; 'received:192.168.1.3': 0.29; 'function': 0.30; "skip:' 20": 0.32; 'could': 0.32; 'to:addr:python-list': 0.33; 'list': 0.35; 'faster': 0.35; 'expected': 0.35; 'but': 0.36; 'characters': 0.36; 'method': 0.36; 'subject:with': 0.36; 'should': 0.36; 'being': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'remove': 0.61; 'kind': 0.61; 'results': 0.65; 'header:Reply-To:1': 0.68; 'applying': 0.69; 'reply-to:no real name:2**0': 0.72; 'special': 0.73; 'reply- to:addr:python.org': 0.84; 'subject:Special': 0.93 |
| X-CM-Score | 0.00 |
| X-CNFS-Analysis | v=2.0 cv=TdYURGsh c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=O2Kvzccb_dQA:10 a=ncDgT-KrJSIA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=IcVb3SNEEWQA:10 a=mwrvXOmflPqxNWYDNL0A:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 |
| X-AUTH | mrabarnett:2500 |
| Date | Tue, 27 Nov 2012 17:54:23 +0000 |
| From | MRAB <python@mrabarnett.plus.com> |
| User-Agent | Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: How to sort list of String without considering Special characters and with case insensitive |
| References | <e6a4709c-ded7-4092-8822-e3ac02b66271@googlegroups.com> |
| In-Reply-To | <e6a4709c-ded7-4092-8822-e3ac02b66271@googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| Reply-To | python-list@python.org |
| 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.315.1354039050.29569.python-list@python.org> (permalink) |
| Lines | 43 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1354039050 news.xs4all.nl 6983 [2001:888:2000:d::a6]:55331 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:33974 |
Show key headers only | View raw
On 2012-11-27 17:31, 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
>
(I'm using Python 3.)
The .sort method accepts a 'key' argument, which lets you pass a
function that transforms the value being sorted before comparison:
>>> def make_key(string):
return string.replace('_', '').upper()
>>> list1 = ['test1_two', 'testOne', 'testTwo', 'test_one']
>>> list1.sort(key=make_key)
>>> list1
['test1_two', 'testOne', 'test_one', 'testTwo']
I don't know how you define 'special'.
You could remove any characters which are special or keep any
characters which are not special, depending on how many characters are
defined as 'special':
from string import ascii_letters
# Sets are faster for this kind of thing.
ascii_letters = set(ascii_letters)
def make_key(string):
return ''.join(c for c in string if c in ascii_letters).upper()
list1 = ['test1_two', 'testOne', 'testTwo', 'test_one']
list1.sort(key=make_key)
print(list1)
# Output is: ['testOne', 'test_one', 'test1_two', 'testTwo']
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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