0

I'm trying to find the median value of a list of lists. I am able to do it for a normal list but unsure how to do it for a list of lists such as below:

norm_row_list = [[0.0, 0.0, 0.0, 0.0, 0.17795385258743912, 0.0677184881194031], [0.5884873389626358, 0.3198112142984678, 0.8174664500409363, 0.6671642766717503, 0.32139709168355723, 1.0], [0.6396995276767564, 0.688061628145554, 0.6534818573661288, 0.019976903098259637, 0.0, 0.27760511005151], [0.7329666273317014, 0.9057703742992778, 0.7434609459640676, 0.8374816423664811, 0.016863091072428376, 0.0], [0.4842313879485761, 0.4510016620800218, 0.625429283659689, 0.38608275062838593, 0.1259237700382603, 0.9794600361121459]]

Currently using this code

n = len(norm_row_list)
s = sorted(norm_row_list)
median = [float(sum(s[n//2-1:n//2+1])/2.0, s[n//2])[n % 2] if n else None]
print(median)

But got error

    median = [float(sum(s[n//2-1:n//2+1])/2.0, s[n//2])[n % 2] if n else None]
TypeError: unsupported operand type(s) for +: 'int' and 'list'
IrrupTor
  • 117
  • 7
  • 1
    `I am able to do it for a normal list` --> then why don't you flatten your list of lists? See https://stackoverflow.com/questions/952914/how-to-make-a-flat-list-out-of-list-of-lists. – Dušan Maďar Sep 19 '19 at 06:17
  • sum(s[n//2-1:n//2+1]), you are trying to sum up two lists – Sosel Sep 19 '19 at 06:17
  • 1
    Possible duplicate of [averaging list of lists python](https://stackoverflow.com/questions/10919664/averaging-list-of-lists-python) – Kostas Charitidis Sep 19 '19 at 06:19
  • You can do this `sum(*s[n//2-1:n//2+1])`, to get rid of that error. What you are trying to do with `float()`? Check that once. – shaik moeed Sep 19 '19 at 06:27
  • Average includes mean, median, mode. This question is about median - the average one seems to have been targeted as mean. So, while they could be duplicates in practice they don't seem to be? – jtlz2 Sep 19 '19 at 09:26

2 Answers2

1

Actually you have multiple ways, the simplest one would be just to use numpy:

import numpy as np
m = np.median(norm_row_list)
print(m)

If you want to unpack your list of list, you can do it with a nested list comprehension:

import numpy as np
l = l = [i for x in norm_row_list for i in x]
m = np.median(l)
print(m)

But I suggest to look into itertools and learn this library:

import itertools
import numpy as np
l =  list(itertools.chain(norm_row_list))
m = np.median(l)
print(m)

Of course you can replace np.median with your calculation, but I suggest you to put it in a function to make your code mode compact and readable.

mucio
  • 7,014
  • 1
  • 21
  • 33
  • 1
    yes, copy&past after few ctrl+z gives bad results, but actually it should be `l = [i for x in norm_row_list for i in x]` – mucio Sep 19 '19 at 06:37
0

Flatten your list of lists as described in How to make a flat list out of list of lists and then use statistics module to calculate median.

import itertools
import statistics

norm_row_list = [...]

flat_norm_row_list = list(itertools.chain(*norm_row_list))
flat_norm_row_list_median = statistics.median(flat_norm_row_list)
print(flat_norm_row_list_median)
Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64