简体   繁体   English

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

[英]How to print two strings (large text) side by side in python?

How do I write code in Python to do this?我如何用 Python 编写代码来做到这一点? I want to read two strings that are multiple lines and much text, mainly to compare how similar they are (qualitatively.)我想读取两个多行多文本的字符串,主要是为了比较它们的相似程度(定性)。

    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)

Output:输出:

    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)     

Here's an approach using slicing:这是使用切片的一种方法:

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)

Output:输出:

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)

This can be generalized to work on any number of strings:这可以推广到适用于任意数量的字符串:

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

Output:输出:

aaaaa bbbbb ccccc
aaa   bbbbb cc
      bbbb

Note that if you're looking for more capability than this, Python has an industrial-strength solution for general diffing called difflib .请注意,如果您正在寻找比这更多的功能,Python 有一个工业强度的通用差异解决方案,称为difflib

This is what I've done.这就是我所做的。 Let me know if there is a better way!让我知道是否有更好的方法!

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

Another approach in which you slice and zip the two strings in order to print them side by side is this:另一种将两个字符串切片并压缩以并排打印的方法是:

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)

Output:输出:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM