Python---Sorted

Source: Internet
Author: User

# sorted# Sort Algorithm # sort is also the algorithm that is often used in the program # regardless of whether you use a bubbling sort or a quick sort, the core of the sort is to compare the size of the two elements # if it's a number, we can compare it directly, but if it's a string or two dict, it doesn't make sense to compare the math size directly, Therefore, the process of comparison must be abstracted out by function # Python's built-in sorted () function to sort the list L = sorted ([5, -12, 9, -21]) print (l) # In addition, the sorted () function is also a higher-order function, It can also receive a key function to implement a custom sort # sorted by absolute size L = sorted ([5, -12, 9, -21], key=abs) print (l) # Key The function specified will be applied to each element of the list, Sorted by the result returned by the key function # string Sort L = sorted ([' Bob ', ' about ', ' Zoo ', ' credits ']) print (l) # By default, the string is sorted by the size of ASCII, because ' Z ' < ' A ', the result capital letter Z will be ranked in the front of lowercase a # string ignoring case, alphabetical order L = sorted ([' Bob ', ' about ', ' Zoo ', ' credits '], Key=str.lower) print (l) # reverse Sort L = Sorted ([' Bob ', ' about ', ' Zoo ', ' credits '], Key=str.lower, reverse=true) print (L) # High-order functions are powerful in abstraction, and the core code can be kept concise


Python---Sorted

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.