14. Python custom sorting and two sort methods

Source: Internet
Author: User

Data_list = []data_list.append ({' Softname ': ' 1 ', ' Version ': ' 1.2.2.2 '}) data_list.append ({' Softname ': ' 7 ', ' Version ': ' 1.2.2.2 '}) data_list.append ({' Softname ': ' 5 ', ' Version ': ' 1.2.2.2 '}) data_list.append ({' Softname ': ' 2 ', ' Version ': ' 1.2.2.2 '}) data_list.append ({' Softname ': ' 3 ', ' Version ': ' 1.2.2.2 '}) data_list.append ({' Softname ': ' 9 ', ' Version ': ' 1.2.2.2 '}) #升序data_list. Sort (Key=lambda obj:obj.get (' Softname '), reverse=false) print data_list#[{' softname ': ' 1 '}, { ' Softname ': ' 2 '}, {' Softname ': ' 3 '}, {' Softname ': ' 5 '}, {' Softname ': ' 7 '}, {' Softname ': ' 9 '}] #降序data_list. Sort (key= Lambda obj:obj.get (' softname '), reverse=true) print data_list#[{' softname ': ' 9 '}, {' Softname ': ' 7 '}, {' Softname ': ' 5 '}, {' Softname ': ' 3 '}, {' Softname ': ' 2 '}, {' Softname ': ' 1 '}] to sort list, Python provides two method method 1. Sort by the list's built-in function List.sort List.sort (Func=none, Key=none, Reverse=false) Python instance:123456>>> list = [2,5,8,9,3] >>> list [2,5,8,9,3] >>> list.sort () >>> lis t [2, 3, 5, 8, 9] Method 2. Sort by the sequence type function sorted (list) (from 2.4 start) Python instance:12345>>> list = [2,5,8,9,3] >>> list [2,5,8,9,3] >>> sorted (list) [2, 3, 5, 8, 9] The difference between the two methods: sorted (list) Returns an object that can be used as an expression. The original list does not change, creating a new sorted list object. List.sort () does not return an object, changing the original list. Other sort instances: Instance 1: Forward Sort 1234>>>l = [2,3,1,4]>>>l.sort () >>>l>>>[1,2,3,4] Instance 2: Reverse Sort 1234>>>l = [2,3,1,4]>>>l.sort (reverse=true) >>>l>>>[4,3,2,1] Instance 3: Sort the second keyword 1234>>>l = [(' B ', 6), (' A ', 1), (' C ', 3), (' d ', 4)]>>>l.sort (Lambda x,y:cmp (x[1],y[1])) >>>l >>>[(' A ', 1), (' C ', 3), (' d ', 4), (' B ', 6)] Instance 4: Sort the second keyword 1234>>>l = [(' B ', 6), (' A ', 1), (' C ', 3), (' d ', 4)]& Gt;>>l.sort (Key=lambda x:x[1]) >>>l>>>[(' A ', 1), (' C ', 3), (' d ', 4), (' B ', 6)] Instance 5: Sort the second keyword 1234 5>>>l = [(' B ', 2), (' A ', 1), (' C ', 3), (' d ', 4)]>>>import Operator>>>l.sort (key= Operator.itemgetter (1)) >>>l>>>[(' A ', 1), (' B ', 2), (' C ', 3), (' d ', 4)] Instance 6: (DSU method: Decorate-sort-undercorate) 123456>>>l = [(' B ', 2), (' A ', 1), (' C ', 3), (' d ', 4)]>>>a = [(x[1],i,x) for i,x in enumerate (  L)] #i can confirm the stable sort>>>a.sort () >>>l = [s[2] for S in a]>>>l>>>[(' A ', 1), (' B ', 2), (' C ', 3), (' d ', 4)] above gives the method of ordering the list in 6, where instance 3.4.5.6 can be used to sort an item in list item as the comparison keyword. Comparison of efficiencies: CMP < DSU < key by experiment , Method 3 is slower than method 6, method 6 is slower than Method 4, Method 4 and Method 5 are basically quite a few keywords comparison sort: instance 7:1234>>>l = [(' d ', 2), (' A ', 4), (' B ', 3), (' C ', 2)]>>> L.sort (Key=lambda x:x[1]) >>> l>>>[(' d ', 2), (' C ', 2), (' B ', 3), (' A ', 4)] we see that at this time the sort of L is ordered only by the second keyword, What if we want to order the second keyword and then sort it with the first keyword? There are two methods of example 8:1234>>> L = [(' d ', 2), (' A ', 4), (' B ', 3), (' C ', 2)]>>> L.sort ( Key=lambda x: (X[1],x[0])) >>> l>>>[(' C ', 2), (' d ', 2), (' B ', 3), (' A ', 4)] instance 9:1234>>> L = [(' d ', 2), (' A ', 4), (' B ', 3), (' C ', 2)]>>> L.sort (Key=operator.itemgetter (1,0)) >>> l>>>[(' C ', 2), ( ' d ', 2), (' B ', 3), (' A ', 4)] Why does instance 8 work? The reason is that the comparison of the tuple is from left to right, compares the first, if equal, compares the second 

14. Python custom sorting and two sort methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.