Python learning-Sorts the items of a dictionary based on the size of the values in the dictionary.

Source: Internet
Author: User
Tags sorts

First, generate a random dictionary:

From random import Randint
D={x:randint (60,99) for x in "ABCDEFG"} # {' A ': +, ' B ': $, ' C ': "All, ' d ':", ' e ': ", ' F ': $, ' G ': 88}

In the first way, the subclasses of collections are sorted from large to small counter:
From collections Import Counter
D={x:randint (60,99) for x in "ABCDEFG"}
C=counter (d). Most_common () #返回一个列表, in accordance with the value of dict from large to small sort [(' B ', '), (' G ', "), (' d ',"), (' E ', "), (' A '," five "), (' C ', 62) , (' f ',.)]
Print (c)

The second way, from small to large sort. General idea: The sorted function can sort the list [] from small to large, and for the dictionary {}dict,sorted function to compare only dict key, so the dict is adjusted to deform.
1, using the zip function to convert dict into a list, each element of the list is a tuple,The tuple size comparison principle is that the first element is compared first and the second one is the same in comparison.
   2. Use the sorted function to sort the converted list.
Z=zip (D.values (), D.keys ()) #[(A (), ' a '), ((a), (' B '), ((), ' C '), ((a), (' d '), (+, ' e '), (+, ' F '), ((a), ' G '), then orted sort
Sorted (z)

The Third Way: [(A1,B1,C1), (A2,B2,C2), (A3,B3,C3), (A4,B4,C4) ...] The sorted function allows you to sort the specified elements of this collection.
Sorted (D.items (), Key=lambda x:x[1]), the first argument is a list that needs to be sorted, and the second parameter is the order of the specified key (the element of each item in the list).
Explanation of this code: D.items () returns a list [(' A ', ' ","), (' B ', ' G '), (' C ', ' + '), (' d ', '), (' E ', ' + '), (' F ', ' 88 ')
Sorted will iterate over the list of D.items (), and each element in the list, that is, each tuple () as x passes into the anonymous function lambda x: x[1], the function returns a value of x[1],
namely Key=x[1]=tuple () [1]= (' A ', 74) [1], in other words, the second element of each item in the table is sorted (the first one is to X[0]).

Python learning-Sorts the items of a dictionary based on the size of the values in the dictionary.

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.