Search code examples
arraysstringmedian

How to get Median string value from array with multiple strings in Python?


I have array, which holds values like for example 1's and 0's. like this:

['00100','00101','00101','01100','01001','00101']

And I want to get median of each char in each string of array:

return would be something like this:

'00101'

If you can give example in Python it would be great.


Solution

  • Python has natural idioms for this sort of problem. The idea is to split things into nice digestible chunks of all the 0-th chars, all the 1-th chars, all the 2-th chars, ... etc. (the zip(*a) part); then figure out the median for each chunk (the sorted(d)[len(a)/2] part); and then to join those chunks back together again. That gives us the one-liner:

    def findMedians(a):
        return ''.join(sorted(d)[len(a)/2] for d in zip(*a))
    
    print findMedians(['00100','00101','00101','01100','01001','00101'])
    # 00101
    print findMedians(['1210', '2501', '1234'])
    # 1211
    

    If you find this a bit confusing, try:

    print zip('123','456','789')
    a = ['123','456','789']
    print zip(*a)
    

    to see what the '*' operator is doing here.