如何将矩阵(或列表列表)与白色空间和字母组合到字符串中



如何将此数组组合到一个字符串中?

array(['  HHHHHHH HHHHHHHHHHH       HHHHHHHHHHHHHHHHHHH    ',
       ' E       E               EEE                       ',
       '                     TT                            ',
       '                       CC                      CCCC'])

结果应该像这样:

   result = 'EHHHHHHHEHHHHHHHHHHHTTCCEEEHHHHHHHHHHHHHHHHHHHCCCC'

一种快速的方法是使用 zip按字符来处理字符,然后在每个位置上最大化;

arr = ['  HHHHHHH HHHHHHHHHHH       HHHHHHHHHHHHHHHHHHH    ',
       ' E       E               EEE                       ',
       '                     TT                            ',
       '                       CC                      CCCC']
''.join(max(x) for x in zip(*arr))
' EHHHHHHHEHHHHHHHHHHHTTCCEEEHHHHHHHHHHHHHHHHHHHCCCC'

类似的东西?

array =['  HHHHHHH HHHHHHHHHHH       HHHHHHHHHHHHHHHHHHH    ',
        ' E       E               EEE                       ',
        '                     TT                            ',
        '                       CC                      CCCC']
result = []
for pos in zip(*array):                   # create tuples of chars from the same index in all strings
    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
result = ''.join(result)                  # convert back to string
print result

哪个打印

EHHHHHHHEHHHHHHHHHHHTTCCEEEHHHHHHHHHHHHHHHHHHHCCCC

这是一个numpy解决方案:

>>> 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')

最新更新