A simple tutorial using the sorted () function in Python _python

Source: Internet
Author: User

Sorting algorithm

Sorting is also an algorithm that is often used in programs. Whether you use bubble sort or quick sort, the core of the sort is to compare the size of two elements. If it's a number, we can compare it directly, but what if it's a string or two dict? There is no point in directly comparing the mathematical size, so the process of comparison must be abstracted from the function. Usually stipulates that for two elements x and Y, if you think X < Y, then return-1, if you think x = y, then return 0, and if you think X > Y, then return 1, so that the sort algorithm doesn't care about the specific comparison process, but rather is sorted directly according to the result of the comparison.

Python's built-in sorted () function allows you to sort the list:

>>> sorted ([5, 9,,])
[5, 9, 12, 21, 36]

In addition, the sorted () function is also a higher-order function that can also receive a comparison function to implement a custom sort. For example, if you want to sort backwards, we can customize a reversed_cmp function:

def reversed_cmp (x, y):
  if x > y:
    return-1
  if x < y: return
    1 return
  0

When you pass in the custom comparison function reversed_cmp, you can sort in reverse order:

>>> sorted ([5, 9, O], reversed_cmp)
[36, 21, 12, 9, 5]

Let's look at a string ordering example:

>>> sorted ([' Bob ', ' about ', ' Zoo ', ' Credit '])
[' Credit ', ' Zoo ', ' about ', ' Bob ']

By default, the string is sorted according to the size of ASCII, and because of the ' Z ' < ' a ', the result is that uppercase Z is ranked in front of the lowercase letter A.

Now, we propose that the sort should ignore case, sorted alphabetically by alphabetical order. To implement this algorithm, you do not have to change the existing code greatly, as long as we can define the case-insensitive comparison algorithm:

def cmp_ignore_case (S1, S2):
  u1 = S1.upper ()
  U2 = S2.upper ()
  if U1 < u2:
    return-1
  if u1 > u2:
   return 1 return
  0

Ignoring the case to compare two strings, in effect, is to first make the string uppercase (or both lowercase), and then compare.

In this way, we pass the above comparison function to sorted to achieve the sort of ignore case:

>>> sorted ([' Bob ', ' about ', ' Zoo ', ' credit '], cmp_ignore_case)
[' About ', ' Bob ', ' Credit ', ' Zoo ']

As can be seen from the example above, the abstraction of higher-order functions is very powerful, and the core code can be kept very concise.

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.