How does Python obtain the median of a List?

Source: Internet
Author: User
This article describes how to use python to obtain the median of a list through text and example code. This article is very detailed. if you need it, you can refer to it. Preface

Median is a value that divides a set of values into equal upper and lower parts. If the number of data in the list is an odd number, the data in the middle of the list is the median of the list data. if the number of data in the list is an even number, then the arithmetic mean of the two data in the middle of the list is the median of the list data. In this task, you will get a non-empty array (X) containing natural numbers ). You must divide it into the upper and lower parts and find the median.

Input: an integer (int) list (list) as an array.

Output: The median of the array (int, float ).

Example

get_median([1, 2, 3, 4, 5]) == 3get_median([3, 1, 2, 5, 3]) == 3get_median([1, 300, 2, 200, 1]) == 2get_median([3, 6, 20, 99, 10, 15]) == 12.5

How to use:Median is applied in probability theory and statistics. it has significant value in partial state distribution. For example, we want to know people's average wealth from a group of data-100 people earn $100 a month, and 10 people earn $1,000,000 a month. If we calculate the average value, we get $91000. This is a strange value that does not show us the real situation at all. So in this case, median will give us more useful values and better descriptions.

Prerequisites: 1 < len(data) ≤ 1000 all(0 ≤ x < 10 ** 6 for x in data)

Common method:

Sort the list, and calculate the median for different cases where the list length is an odd or even number.

Def get_median (data): data = sorted (data) size = len (data) if size % 2 = 0: # determine if the list length is an even number median = (data [size // 2] + data [size // 2-1]) /2 data [0] = median if size % 2 = 1: # judge that the list length is an odd number median = data [(size-1) // 2] data [0] = median return data [0]

Best practice:

This solution is clever. it uses the inverse number sum and the negative index of the list to obtain the median of the list.

Pairreturn (data[half] + data[~half]) / 2 Explanation:

After sorting, the sequence [1, 2, 3, 4, 5, 6] is obtained. the list length is an even number. the median value is determined by the two elements 3 (index 2) and 4 (index 3) in the middle of the list. The negative index of Element 4 IS-3, which is the reverse number of index 2.

After sorting, the sequence [1, 2, 3, 4, 5] is obtained. the list length is an odd number. the median value is determined by the intermediate element 3 (index 2 and negative index-3) of the list. The code is still correct.

   def get_median(data):   data.sort()   half = len(data) // 2   return (data[half] + data[~half]) / 2

For more information about how to obtain the median of a List in Python, see The PHP Chinese website!

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.