如何在python中并排打印两个字符串(大文本)?

如何在python中并排打印两个字符串(大文本)?,python,string,printing,Python,String,Printing,如何用Python编写代码来实现这一点?我想读两个多行多文本的字符串,主要是比较它们有多相似(定性) 输出: 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

如何用Python编写代码来实现这一点?我想读两个多行多文本的字符串,主要是比较它们有多相似(定性)

输出:

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

这就是我所做的。如果有更好的方法,请告诉我

    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))
def逐面打印(sa、sb):
def填充线(供体、接收器、lim):
而捐赠者和len(接收者)len(线路b):
行附加([''表示范围内的k(nb)])
而len(lines_b)>len(lines_a):
行\u a.追加([''表示范围内的k(na)])
断言len(行a)==len(行b)
行a=[''。在行a中为l连接(l)
行_b=[''。在行_b中为l连接(l)]
lines=[lines_a[k]+''+lines_b[k]表示范围内的k(len(lines_a))]
打印('\n'.连接(行))

以下是一种使用切片的方法:

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 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)     
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)
我想读这些文本sid我真的想读这些他
e并排,看看有多相似,重新文本并排,看看h
他们真的很相似
(续)
这可以推广到处理任意数量的字符串:

def side_by_side(strings, size=30, space=4):
    strings = list(strings)
    result = []

    while any(strings):
        line = []

        for i, s in enumerate(strings):
            line.append(s[:size].ljust(size))
            strings[i] = s[size:]

        result.append((" " * space).join(line))
    
    return "\n".join(result)

if __name__ == "__main__":
    strings = "aaaaaaaa", "bbbbbbbbbbbbbb", "ccccccc"
    print(side_by_side(strings, size=5, space=1))
输出:

    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)     
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)
aaaaa bbbbb ccccc
aaa BBB cc
bbbb


请注意,如果您希望获得比这更多的功能,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)'

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 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)     
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)
退房