Вот простое решение:
>>> x
array([' HHHHHHH HHHHHHHHHHH HHHHHHHHHHHHHHHHHHH ',
' E E EEE ',
' TT ',
' CC CCCC'],
dtype='<U51')
>>> x.view('u4').reshape(len(x), -1).max(0).view(x.dtype).item(0).strip()
'EHHHHHHHEHHHHHHHHHHHTTCCEEEHHHHHHHHHHHHHHHHHHHCCCC'
Время:
f_pp 5.941 us
f_tb 27.473 us
f_ji 21.265 us
Код для получения времени:
import numpy as np
from timeit import timeit
x = np.array([' HHHHHHH HHHHHHHHHHH HHHHHHHHHHHHHHHHHHH ',
' E E EEE ',
' TT ',
' CC CCCC'])
def f_pp():
return x.view('u4').reshape(len(x), -1).max(0).view(x.dtype).item(0).strip()
def f_tb():
result = []
for pos in zip(*x): # create tuples of chars from the same index in all string
char = ''.join(pos).replace(' ', '') # remove all space chars
if char: # if there's anything left (ie. skip the char at index 0)
result.append(char[-1]) # then append the char from the array closest to the bottom
return ''.join(result) # convert back to string
def f_ji():
return ''.join(max(y) for y in zip(*x)).strip()
for f in (f_pp, f_tb, f_ji):
print(f.__name__, f'{timeit(f, number=1000) * 1000:>6.3f} us')