Как напечатать две строки (большой текст) рядом в Python? - PullRequest
0 голосов
/ 20 ноября 2018

Как мне написать код на Python для этого?Я хочу прочитать две строки, которые состоят из нескольких строк и большого количества текста, главным образом, чтобы сравнить, насколько они похожи (качественно).

    s1 = 'I want to read these texts side by side and see how similar they really are'
    s2 = 'I really want to read these here texts side by side to see how similar they are (qualitatively)'
    print_side_by_side(s1,s2)

Вывод:

    I want to read these texts side by side and see ho   I really want to read these here texts side by sid
    w similar they really are                            e to see how similar they are (qualitatively)     

Ответы [ 3 ]

0 голосов
/ 21 ноября 2018

Вот подход с использованием нарезки:

def print_side_by_side(a, b, size=30, space=4):
    while a or b:
        print(a[:size].ljust(size) + " " * space + b[:size])
        a = a[size:]
        b = b[size:]

s1 = 'I want to read these texts side by side and see how similar they really are'
s2 = 'I really want to read these here texts side by side to see how similar they are (qualitatively)'
print_side_by_side(s1,s2)

Вывод:

I want to read these texts sid    I really want to read these he
e by side and see how similar     re texts side by side to see h
they really are                   ow similar they are (qualitati
                                  vely)

Попробуйте!

0 голосов
/ 21 ноября 2018

Другой подход, при котором вы нарезаете и сжимаете две строки, чтобы напечатать их рядом, это:

s1 = 'I want to read these texts side by side and see how similar they really are'
s2 = 'I really want to read these here texts side by side to see how similar they are (qualitatively)'

maxChars = 40
maxLength = max(len(s1),len(s2))

s1 = s1.ljust(maxLength," ")
s2 = s2.ljust(maxLength," ")

s1 = [s1[i:i+maxChars] for i in range(0,len(s1),maxChars)]
s2 = [s2[i:i+maxChars] for i in range(0,len(s2),maxChars)]

for elem1, elem2 in zip(s1,s2):
    print(elem1.ljust(maxChars," "), end="    ")
    print(elem2)

Вывод:

I want to read these texts side by side     I really want to read these here texts s
and see how similar they really are         ide by side to see how similar they are 
                                            (qualitatively)
0 голосов
/ 20 ноября 2018

Это то, что я сделал.Дайте мне знать, если есть лучший способ!

    def print_side_by_side(sa,sb):

        def populate_line(donor, receiver, lim):
            while donor and len(receiver)<lim:
                new_char = donor.pop(0)
                if new_char =='\n':
                    break
                receiver.append(new_char)
            while len(receiver) < lim:
                receiver.append(' ')

        la = list(sa)
        lb = list(sb)

        # number of chars per line; may have to tweak
        nline = 100
        na = nline//2
        nb = nline-na

        lines_a = []
        lines_b = []
        while la or lb:

            line_a = []
            line_b = []

            populate_line(la,line_a,na)
            populate_line(lb,line_b,nb)

            lines_a.append(line_a)
            lines_b.append(line_b)


        while len(lines_a) > len(lines_b):
            lines_b.append([' ' for k in range(nb)])
        while len(lines_b) > len(lines_a):
            lines_a.append([' ' for k in range(na)])

        assert len(lines_a) == len(lines_b)

        lines_a = [''.join(l) for l in lines_a]
        lines_b = [''.join(l) for l in lines_b]

        lines = [lines_a[k] + '   ' + lines_b[k] for k in range(len(lines_a))]


        print('\n'.join(lines))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...