RE: How to sort list of String without considering Special characters and with case insensitive
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. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to sort list of String without considering Special characters and with case insensitive
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'] -- http://mail.python.org/mailman/listinfo/python-list
How to sort list of String without considering Special characters and with case insensitive
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to sort list
In article <[EMAIL PROTECTED]>, "Lad" <[EMAIL PROTECTED]> wrote: > I have a list of emails and I would like to sorted that list by domains > E.g. > If the list is > > Emails=['[EMAIL PROTECTED]','[EMAIL PROTECTED]','[EMAIL PROTECTED]','[EMAIL > PROTECTED]',] > > after sorting I would like to have > Emails=['[EMAIL PROTECTED]','[EMAIL PROTECTED]','[EMAIL PROTECTED]','[EMAIL > PROTECTED]',] > > What is the best/easiest way? One reasonable option is to use the .sort() method of a list: Emails.sort(key = lambda s: list(reversed(s.split('@' The "key" parameter specifies how to obtain a sort key from each element in the source list. Cheers, -M -- Michael J. Fromberger | Lecturer, Dept. of Computer Science http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA -- http://mail.python.org/mailman/listinfo/python-list
Re: How to sort list
Fredrik Lundh wrote: > note that DSU is built into Python these days: > > L.sort(key=transform) Sweet, thanks for the hint. Cheers, -- Klaus Alexander Seistrup http://klaus.seistrup.dk/ -- http://mail.python.org/mailman/listinfo/python-list
Re: How to sort list
Klaus Alexander Seistrup wrote: > Decorate-sort-undecorate? > > #v+ > > array = [] > > for addr in Emails: > (user, domain) = addr.split('@') > array.append((domain, user, addr)) > # end for > > array.sort() > > SortedEmails = [addr for (user, domain, addr) in array] > > #v- note that DSU is built into Python these days: L.sort(key=transform) so you could use e.g. Emails.sort(key=lambda s: s.partition("@")[::-1]) also see: http://preview.tinyurl.com/yc3qak http://effbot.org/zone/python-list.htm#sorting -- http://mail.python.org/mailman/listinfo/python-list