Python permutation functions: sort, sorted

Source: Internet
Author: User
Tags sorted by name python list

Sort function Description: sort () and sorted () are all part of the Python list sorting method

Difference: sort () is a permanent arrangement that changes the list directly; sorted is a temporary arrangement that produces a new sequence.

# Sorted ()  print sorted ([5, 2, 3, 1, 4]) [1, 2, 3, 4, 5]#sort ()>>> L = [5, 2, 3, 1, 4]>>> l.sort ()print  l[1, 2, 3, 4, 5]

The following is an introduction to commonly used sorted:

Python's built-in sorted () function can sort a list:

>>> sorted ([8,3,8,11,-2]) [-2, 3, 8, 8, 11]

Since it is a higher-order function, it can also accept a key function to implement a custom sort, such as sorting by absolute size:

>>> sorted ([8,3,8,11,-9],key=abs) [3, 8, 8,-9, 11]

The function specified by key will be applied to each element in the list, sorted according to the result returned by the key function.

to see the problem with string sorting :

>>> sorted ([' ABC ', ' abc ', ' CBA ', ' BAC ']) [' ABC ', ' CBA ', ' abc ', ' BAC ']

By default, for strings, the sorted function is sorted by the size of ASCII, because C<a, uppercase C, precedes lowercase a.

If we want to achieve a sort that ignores case, we just need to change the key function:

>>> sorted ([' ABC ', ' abc ', ' CBA ', ' BAC '],key=str.lower) [' abc ', ' abc ', ' BAC ', ' CBA ']

Further, to reverse order, you can pass in the third parameter reverse=true:

>>> sorted ([' ABC ', ' abc ', ' CBA ', ' BAC '],key=str.lower,reverse=true) [' CBA ', ' BAC ', ' abc ', ' ABC ']

Sorted () The key to sorting is to implement a mapping function!

Practice:

Use a group of tuples to represent student names and grades:L = [(‘Bob‘, 75), (‘Adam‘, 92), (‘Bart‘, 66), (‘Lisa‘, 88)]

Requirements: sorted by name and score, respectively

#alphabetical order by name>>> L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]>>>defBy_name (t):returnt[0] #t [0] means name >>> L2 = sorted (L,key =by_name)>>>Print(L2) [('Adam', 92), ('Bart', 66), ('Bob', 75), ('Lisa', 88)]#按成绩低到高排序>>>defBy_score (t):returnT[1] #t [1] indicates a score >>> L2 = sorted (L,key =By_score)>>>Print(L2) [('Bart', 66), ('Bob', 75), ('Lisa', 88), ('Adam', 92)]

Python permutation functions: sort, sorted

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.