here is my problem. I got a list of lists like this:
[
[1, 1, 1, 18, 35, 35, 70, 133, 280],
[1, 1, 1, 53, 90, 101, 130, 148, 178],
[1, 1, 1, 18, 35, 133, 133, 164],
[1, 1, 1, 18, 101, 108],
[1, 1, 18, 36, 86, 118, 126]
]
The list can have up to 9 items, and all the sublists contain at least 5 items. I need to find the median for every number at the nth position per each list, ignoring lists too short (otherwise I would be out of index).
I tried d = [item[i] for item in c]
, but it fails for i > 5 (when the lists start to have a different length). Any idea how to solve the problem?
The output for the solution in the example should be:
[median_numbers_1st_position,
median_numbers_2nd_position,
median_numbers_3rd_position,
median_numbers_4th_position,
median_numbers_5th_position,
median_numbers_6th_position,
median_numbers_7th_position,
median_numbers_8th_position,
median_numbers_9th_position
]
Thanks a lot for the help!
i
exactly?for i in range(5)
? Please provide a minimal reproducible example.[np.median(i) for i in [[k for k in j if k is not None] for j in itertools.zip_longest(*lst)]]
output:[1.0, 1.0, 1.0, 18.0, 86.0, 108.0, 128.0, 148.0, 229.0]