Python standard library: Built-in function sorted (iterable[, key][, reverse])

Source: Internet
Author: User
Tags iterable

This function is implemented to sort the iterable of an iterative object . The optional parameter key is a function of the comparison key;Reverse is a Boolean value that indicates whether to reverse-arrange the items in the object.

Example:

#sorted () print (sorted ([5, 2, 3, 1, 4])) print (sorted ({1: ' D ', 2: ' B ', 3: ' B ', 4: ' E ', 5: ' A '}, reverse = True)) print (sorted ("This was a test string from Andrew". Split (), Key=str.lower)) Student_tuples = [        (' John ', ' A ', '),        (' Jane ', ' B ', '), (' Dave ', ' B ', '),        ]print (sorted (Student_tuples, Key=lambda student:student[2])   # sorted by age

The resulting output is as follows:

[1, 2, 3, 4, 5]

[5, 4, 3, 2, 1]

[' A ', ' Andrew ', ' from ', ' was ', ' string ', ' test ', ' this ']

[(' Jane ', ' B ', ' + '), (' John ', ' A ', '), (' Dave ', ' B ', 100)]



Cai Junsheng qq:9073204 Shenzhen

Python standard library: Built-in function sorted (iterable[, key][, reverse])

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.