Dictionary sort in Python

Source: Internet
Author: User
Tags function prototype

such as dictionary dic={' a ': 1, ' F ': 2, ' C ': 3, ' H ': 0}; to sort it:

function Prototype: sorted (dic,value,reverse);

  1. DIC is a comparative function;
  2. Value is the comparison object (key or value);
  3. Reverse: Indicate ascending or descending, true--descending, false--ascending (default);
    1 Importoperator; 2 #Sort in dictionary3 defsortdict ():4dic={'a': 1,'F': 2,'C': 3,'h': 0};5     #function Prototype: sorted (dic,value,reverse)6     #sort Ascending by key in dictionary7     Print("Press the key to sort the result in ascending order:",8Sorted (Dic.items (), Key=operator.itemgetter (0), reverse=False));9     #sort Descending by key in dictionaryTen     Print("The keys are sorted in descending order:", OneSorted (Dic.items (), Key=operator.itemgetter (0), reverse=True)); A     #sort Ascending by values in a dictionary -     Print("ascending Sort by value results in:", -Sorted (Dic.items (), Key=operator.itemgetter (1), reverse=False)); the     #Sort Descending by a value in a dictionary -     Print("descending Sort by value results in:", -Sorted (Dic.items (), Key=operator.itemgetter (1), reverse=true));

    The result of the operation is:

    1>>>ImportRandmatrix;2>>>sortdict ()3The key is sorted in ascending order: [('a', 1), ('C', 3), ('F', 2), ('h', 0)]4The key is sorted in descending order: [('h', 0), ('F', 2), ('C', 3), ('a', 1)]5Ascending sort by value results in: [('h', 0), ('a', 1), ('F', 2), ('C', 3)]6The descending sort by value results in the following: [('C', 3), ('F', 2), ('a', 1), ('h', 0)]

    Iteritems () function:

  • Iteritems () returns a dictionary key value pair with an iterator object;

  • Compared with item: Items returns a dictionary key value pair as a list
    1>>> dic={'a': 1,'F': 2,'C': 3,'h': 0};2>>>Dic.items ()3Dict_items ([('F', 2), ('h', 0), ('C', 3), ('a', 1)])4>>>Dic.iteritems ()5 Traceback (most recent):6File"<pyshell#34>", Line 1,inch<module>7 Dic.iteritems ()8Attributeerror:'Dict'object has no attribute'Iteritems'9>>>Print(Dic.iteritems ())Ten Traceback (most recent): OneFile"<pyshell#35>", Line 1,inch<module> A     Print(Dic.iteritems ()) -Attributeerror:'Dict'object has no attribute'Iteritems'

    There is no iteritems () function in Python3.4.2, so error;

Dictionary sort in Python

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.