Code Med

My professor wrote this median function, and I do not understand it very well. Can someone explain the part i = len(list)/2 and median = avg() and the else ?

 def avg_list(numbers): sum = 0 for num in numbers: sum += num avg = float(sum)/len(numbers) print avg def median(list): list.sort() if len(list)%2 == 0: #have to take avg of middle two i = len(list)/2 median = avg() else: #find the middle (remembering that lists start at 0) i = len(list)/2 median = list return median 

To add from the example I saw for an even list length:

 def median(s): i = len(s) if not i%2: return (s[(i/2)-1]+s[i/2])/2.0 return s[i/2] 

This works very well, but I don’t understand the last return s[i/2] ?

For the length of the odd list:

 x = [1,2,5,2,3,763,234,23,1,234,21,3,2134,23,54] median = sorted(x)[len(x)/2] 

Since x is an odd-numbered list, is [len(x)/2] a floating-point index? I do not get it completely? Any explanation better than mine is greatly appreciated.

+6
python median
Sep 28 '11 at 5:23
source share
4 answers

We are missing the code here, but we can figure it out.

The comments here are instructive. When we check:

  if len(list)%2 == 0: 

Then we check if the list has an even length. If the list has an even number of members, then there is no true "middle" element, and therefore:

  #have to take avg of middle two i = len(list)/2 median = avg() 

Assume that the function avg () returns the average of two middle elements. Since you did not specify a definition for the avg function, it is possible that it really should be an avg_list function that uses two items from a list.

Now, if the list has an odd length, there is a middle element, and so:

  else: #find the middle (remembering that lists start at 0) i = len(list)/2 median = list 

Now it looks wrong for me too, but I assume that this intention is that it should read:

 median = list[i] 

This means that we are returning the middle element of the list. Since the list is sorted, this middle element is the true median of the list.

Hope this helps!

+5
Sep 28 '11 at 5:35
source share

Why is this very wrong, line by line:

 def median(list): # 1 list.sort() # 2 if len(list)%2 == 0: #have to take avg of middle two i = len(list)/2 # 3 median = avg() # 4 else: #find the middle (remembering that lists start at 0) i = len(list)/2 # 5 median = list # 6 return median 

#1 : It’s a bad idea to give your variables the same name as the data types, namely list .

#2 : list.sort() will change the list that is passed. One would expect a getter like median() not do this.

#4 It calls the avg() function with no arguments, which is completely pointless, even if such a function is defined.

#3 and #5 are evaluated the same regardless of the if branch. Despite this, i never used.

#6 It sets median to the original list , which makes zero sense.




Here's how I would rewrite this (while maintaining clarity):

 def median(alist): srtd = sorted(alist) # returns a sorted copy mid = len(alist)/2 # remember that integer division truncates if len(alist) % 2 == 0: # take the avg of middle two return (srtd[mid-1] + srtd[mid]) / 2.0 else: return srtd[mid] 

In addition, the avg_list() function (which is not used and cannot be used in median() ) can be rewritten as:

 def avg_list(numbers): return float(sum(numbers))/len(numbers) 

sum() is a function that returns the sum of all elements in an iterable version.

+11
Sep 28 2018-11-11T00:
source share

I'm sure he is trying to say: "If the list is odd in size, just take the center element, otherwise take the middle of the center two elements" - but I don’t see that this is what the code actually is.

In particular:

  • It calls the avg() function (not avg_list , note), but without any arguments
  • It ignores the value of i after calculating it in the same way in both branches

Are you sure the complete code should work?

+2
Sep 28 '11 at 5:30
source share

You can also always return the average value of the average auxiliary array of an ordered list: For example, return the average value [4,5] from [1,2,3,4,5,6,7,8], and the average from [5] from [ 1,2,3,4,5, 6,7,8,9].

The python implementation will be:

 def median(a): ordered = sorted(a) length = len(a) return float((ordered[length/2] + ordered[-(length+1)/2]))/2 
+2
Jun 15 '12 at 5:27
source share



All Articles