damon
damon

Reputation: 8467

how to find the middle number in python

Given 3 numbers, I need to find which number lies between the two others.

ie,given 3,5,2 I need 3 to be returned.

I tried to implement this by going thru all three and using if else conditions to check if each is between the other two.But this seems a naive way to do this.Is there a better way?

Upvotes: 8

Views: 72154

Answers (10)

samv
samv

Reputation: 41

If it's just for 3 numbers. First find the max and the min numbers in list. Remove them, the remainder of list is the medium. If you don't want to use the "if-else" or "sorted()" that is.

def mid(a,b,c):
    num = [a,b,c]
    small, big = min(num), max(num)
    num.remove(small)
    num.remove(big)
    return num[0] # return the remainder

Upvotes: 1

Cabbage soup
Cabbage soup

Reputation: 1394

If you wish to avoid sorting, you can do:

def find_median(x):
    return sum(x) - max(x) - min(x)

Upvotes: 0

Michael
Michael

Reputation: 2556

Here is my attempt using a more pythonic version

def median(a):
    sorted_a = sorted(a)
    if len(a) % 2 == 0:
        median = sum(sorted_a[(len(a)//2)-1:(len(a)//2)+1])/2.
    else:
        median = sorted_a[(len(a)-1)//2]

>>> x = [64630, 11735, 14216, 99233, 14470, 4978, 73429, 38120, 51135, 67060]
>>> median(x)
>>> 44627.5
>>> y = [1, 2, 3]
>>> median(y)
>>> 2

Upvotes: 0

jamylak
jamylak

Reputation: 133514

>>> x = [1,3,2]
>>> sorted(x)[len(x) // 2]
2

Upvotes: 16

Zaur Nasibov
Zaur Nasibov

Reputation: 22659

The fastest obvious way for three numbers

def mean3(a, b, c):
    if a <= b <= c or c <= b <= a:
        return b
    elif b <= a <= c or c <= a <= b:
        return a
    else:
        return c

Upvotes: 10

Zhe
Zhe

Reputation: 29

Check this (Suppose list already sorted):

def median(list):
    ceil_half_len = math.ceil((len(list)-1)/2)   # get the ceil middle element's index
    floor_half_len = math.floor((len(list)-1)/2) # get the floor middle element 's index'
    return (list[ceil_half_len] + list[floor_half_len]) / 2

Upvotes: 1

luke14free
luke14free

Reputation: 2539

This is a O(n) implementation of the median using cumulative distributions. It's faster than sorting, because sorting is O(ln(n)*n).

def median(data):
    frequency_distribution = {i:0 for i in data}
    for x in data:
        frequency_distribution[x] =+ 1
    cumulative_sum = 0
    for i in data:
        cumulative_sum += frequency_distribution[i]
        if (cumulative_sum > int(len(data)*0.5)):
            return i

Upvotes: 2

Thanasis Petsas
Thanasis Petsas

Reputation: 4448

What you want is the median. You can use this code below for any number of numbers:

import numpy
numbers = [3,5,2]
median = numpy.median(numbers)

for a custom solution you can visit this page.

Upvotes: 5

David Robinson
David Robinson

Reputation: 78590

You could do

numbers = [3, 5, 2]
sorted(numbers)[1]

Upvotes: 4

Sven Marnach
Sven Marnach

Reputation: 601499

Put them in a list, sort them, pick the middle one.

Upvotes: 23

Related Questions