Python Sort Function Summary

Source: Internet
Author: User
Tags iterable
Sorted functions and Sort methods
  • Sorted (iterable) #原容器不变, returns the list, the container can be set,dict (returns a sorted list of keys)
  • List.sort () #修改原列表
Sorted ():
  • Sorted (iterable, Cmp=none, Key=none, Reverse=false)--New sorted list
  • CMP: comparison function, customizable:
    # sorted by bit size, CMP functions are defined as x > y positive, x < y negative, x = = y is 0f = lambda x,y:1 if x%10 > y%10 Else ( -1 if x%10 < y%10 el SE 0) # Unified into lowercase sort cmp=lambda x,y:cmp (X.lower (), Y.lower ())
  • Key: Sort element function, specifying what list elements to sort by
    # You can sort the string in bits of its number, F is the sort function sorted (["321", "123", "231", "222"], cmp=f, Key=int)
  • Reverse: whether to reverse

L.sort (Cmp=none, Key=none, Reverse=false)-stable sort *in place*;

Similar to sorted, it will only modify the original L

Python Sort Function Summary

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.