Python dictionary sort

Source: Internet
Author: User

Sorted (Dic.items (),key=Lambdaitem:item[0])



Related Knowledge collation

1, sorted ()
Sorted (iterable, Key=none, Reverse=false)
Iterable: An Iterative object
Key: Specifies a function that is used tofromin IterableIn each element of theextracts a property to act as aThe keyword used for comparison. The default value is None.
Reverse: Default is Fale, true when list elements are sorted in reverse order
Returns a new list
RightiterableSort by the function of key

2, Dic.iteMs( )
Dict.items () returns a list of the dictionary's (key, value) tuple pairs

3. Lambda Item:item[0]
   anonymous function, take the No. 0 element of item

Summarize:
Sorted (Dic.items (),key= Lambda item:item[0])
iterable section:theDic.items ()To sort,dic.items () Returns a list of the tuple pairs that the list element is a dictionary. such as [(1, ' A '), (2, ' B ')]
Key part: For a function, this function takes item[0], that is, the list of each element of the No. 0 bit to compare sort. Compare 1, 2 in [(1, ' A '), (2, ' B ')]
  

Python dictionary sort

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.