How to sort a dictionary key value size

Source: Internet
Author: User

In some cases, we need to sort the dictionary key value sizes.
For example, the following conditions:
A dictionary that contains the contents of a country: the number of people.
{' Indonesia ': ' 239870000 ', ' Malaysia ': ' 28401000 '}
Note that the dictionary's key value is the string ' 239870000 ' and cannot be sorted directly by size. We need to convert it into numbers.

Let's do a case. There is a population of countries from 1960 to 2010 in the world. We need to extract the population of 2010 from a large to a small sort.

Reference code:

"' Import a demographic text file '
Import JSON
filename = ' Population_data.json '
with open (filename) as F:
Pop_data = Json.load (f)

"Create an empty dictionary for storing data extracted for 2010 years"
pop_2010 = {}

"Extracted 2010 years of world population, the data in the format of the dictionary into the empty dictionary just created pop_2010"
For pop_dict in Pop_data:
If pop_dict[' year '] = = ' 2010 ':
Try
Country_name = pop_dict[' country name ']
Population = pop_dict[' Value ']
Pop_2010[country_name] = Int (population)
Except ValueError:
Continue

"' Print dictionary, sort ' '
Print (sorted (Pop_2010.items (), key = Lambda item:item[1], reverse = True))

The Pop_2010.items () here is actually converting the dictionary pop_2010 to an iterative object, with the elements of the Iteration object (' Indonesia ', 239870000), (' Malaysia ', 28401000), items () The Key=lambda method converts the elements of a dictionary into tuples, and the lambda expression that corresponds to the key parameter here means that the second element in the selection tuple is used as the comparison parameter (if writing a item:item[0], the first element is selected as the comparison object. That is, the key value as the comparison object. In Lambda X:y, x represents the output parameter, and Y represents the return value of the lambda function, so you can sort the value of the dictionary by this method. Note that the returned value after sorting is a list, and the name value pairs in the original dictionary are converted to tuples in the list.

How to sort a dictionary key value size

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.