慕森王
这是一个numpy的解决方案:>>> xarray([' 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 usf_tb 27.473 usf_ji 21.265 us产生计时的代码:import numpy as npfrom timeit import timeitx = 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 stringdef 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')