简体   繁体   English

如何从子列表中找到中位数的中位数?

[英]How to find median of medians from sublists?

Working on a function which would take a list, keep breaking it down into sublists of 3 items each by finding the median of each sublist, then outputting the final median.处理一个接受列表的函数,通过找到每个子列表的中位数,然后输出最终的中位数,继续将其分解为 3 个项目的子列表。 For example, given the list [99, 42, 17, 7, 1, 9, 12, 77, 15] , the function would break it down to [[99, 42, 17], [7, 1, 9], [12, 77, 15]] then find the medians, [42,7,15] , which would then give the final output of 15 .例如,给定列表[99, 42, 17, 7, 1, 9, 12, 77, 15] ,该函数会将其分解为[[99, 42, 17], [7, 1, 9], [12, 77, 15]]然后找到中位数[42,7,15] ,然后给出最终输出15 Using the same logic, the list [55, 99, 131, 42, 88, 11, 17, 16, 104, 2, 8, 7, 0, 1, 69, 8, 93, 9, 12, 11, 16, 1, 77, 90, 15, 4, 123] should also return 15 , but I keep getting 16 instead.使用相同的逻辑,列表[55, 99, 131, 42, 88, 11, 17, 16, 104, 2, 8, 7, 0, 1, 69, 8, 93, 9, 12, 11, 16, 1, 77, 90, 15, 4, 123]也应该返回15 ,但我一直得到16 Really can't figure out what I am doing wrong.真的无法弄清楚我做错了什么。

Here's what I have so far:这是我到目前为止所拥有的:

def test(lst):
    import numpy as np
    if 1162261467 % len(lst) == 0:
      lst = np.array(lst)
      lst1 = np.split(lst,3)
      lst2 = np.array(lst1).tolist()
      l = [i for x in lst2 for i in x]
      m = np.median(l)
    return m
print (test([55, 99, 131, 42, 88, 11, 17, 16, 104, 2, 8, 7, 0, 1, 69, 8, 93, 9, 12, 11, 16, 1, 77, 90, 15, 4, 123]
))# Outputs 16

This should work:这应该有效:

import numpy as np
def test(lst):

    if 1162261467 % len(lst) == 0:
      lst = np.array(lst)
      lst1 = np.split(lst,len(lst)/3) # <-- you had bug here, here is the correct one
      lst2 = np.array(lst1).tolist()

      l = [np.median(x) for x in lst2] # <-- Another bug is here, here is the corrrect one.
      m = np.median(l)
    return m
print (test([55, 99, 131, 42, 88, 11, 17, 16, 104, 2, 8, 7, 0, 1, 69, 8, 93, 9, 12, 11, 16, 1, 77, 90, 15, 4, 123]
))

Since the starting array is a power of three you can always count on the further arrays being a multiple of three.由于起始数组是 3 的幂,因此您始终可以指望其他数组是 3 的倍数。 This makes it quite concise with numpy:这使得 numpy 变得非常简洁:

import numpy as np

l = np.array([55, 99, 131, 42, 88, 11, 17, 16, 104, 2, 8, 7, 0, 1, 69, 8, 93, 9, 12, 11, 16, 1, 77, 90, 15, 4, 123])

while len(l) > 1:
    l = np.median(l.reshape(-1, 3), axis=1)
    print(l)

print("median:", int(l[0]))

Prints:印刷:

[99. 42. 17.  7.  1.  9. 12. 77. 15.]
[42.  7. 15.]
[15.]
median: 15

This seems to do it.这似乎做到了。 I am kind of mystified by the 1162261467 in your original.我对你原文中的 1162261467 有点困惑。 Ah, its 3^19.啊,它的 3^19。 Also you were just reconstructing the original list in your list comprehension.此外,您只是在列表理解中重建原始列表。

import numpy as np
def test(lst):
    if len(lst) == 3:
        lst = np.array(lst)
        m = np.median(lst)
        return m
    else:
        lst = np.array(lst)
        lst1 = np.split(lst,len(lst)/3)
        lst2 = np.array(lst1).tolist()
        l = [test(l) for l  in lst2]
        return test(l)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM