如何将多个字符串并排打印,并跨越多行以固定的输出宽度。

huangapple go评论72阅读模式
英文:

How to print several strings side-by-side and span multiple lines at a fixed output width

问题

我正在尝试按字符逐个打印三个长字符串(相同长度),并且固定输出宽度为60,可能呈现如下:

aaaaaaaaaaaaa
bbbbbbbbbbbbb
ccccccccccccc
---空行---
aaaaaaaaaaaaa
bbbbbbbbbbbbb
ccccccccccccc

.....

我简化了字符串,使第一个字符串是包含"a"的任意长字符串,第二个字符串包含许多"b",依此类推。可以显示与上述示例相同的多个行块,每个块中,第一行代表字符串1,第二行代表字符串2,等等。由于需要固定的输出宽度,如果长度大于60,打印将继续在下一个三行块中进行。

我的当前代码如下:

for chunk in chunkstring(str1, 60):
    f.write(chunk)
    f.write('\n')
for chunk in chunkstring(str2, 60):
    f.write(chunk)
    f.write('\n')
for chunk in chunkstring(str3, 60):
    f.write(chunk)
    f.write('\n')

然而,结果不正确。它会首先打印出所有的str1,然后是str2,然后是str3。

aaaaaaaaaaaaa
aaaaaaaaaaaaa
aaaaaaaaaaaaa
aaa
---空行---
bbbbbbbbbbbbb
bbbbbbbbbbbbb
bbbbb
---空行---
cccccccccccc
ccc
.....

如果描述不清楚,请突出显示任何模糊之处,以便我可以编辑描述。

英文:

I am trying to print out three long strings (same-length), character-by-character, and with a fixed output width at 60, which may be rendered like:

aaaaaaaaaaaaa
bbbbbbbbbbbbb
ccccccccccccc
---blank line---
aaaaaaaaaaaaa
bbbbbbbbbbbbb
ccccccccccccc

.....

I simplify the strings so that the first string is an arbitrarily long string contains "a"s, the second string contains many "b"s, etc. There could be as many blocks of lines shown above as possible, within each block, the first line stands for string1, second line stands for string2..etc. .And since a fixed output width is required, the printing will continue at the next block of three lines(for example, str1 will continue at the first line of the second block if length>60).

My current code looks like:

 for chunk in chunkstring(str1, 60):
    f.write(chunk)
    f.write('\n')
 for chunk in chunkstring(str2, 60):
    f.write(chunk)
    f.write('\n')
 for chunk in chunkstring(str3, 60):
    f.write(chunk)
    f.write('\n')

However, the result is not correct. It will print out all the str1 first then str2, then str3

  aaaaaaaaaaaaa
  aaaaaaaaaaaaa
  aaaaaaaaaaaaa
  aaaa
  ---blank line---
  bbbbbbbbbbbbb
  bbbbbbbbbbbbb
  bbbbbb
  ---blank line---
  ccccccccccccc
  cccc
  .....

Sorry if not intepretted clearly, please highlight any ambiguation so I can edit the descrption.

答案1

得分: 2

你需要将所有字符串进行分块,然后使用 zip 同时迭代这些块。现在,block 是一个包含每个字符串各自块的 元组

chunks = [chunkstring(s, 60) for s in (str1, str2, str3)]

for block in zip(*chunks):
    for c in block:
        # 写入块中的所有行
        f.write(c)
        f.write('\n')
    # 在块的末尾写入一个空行
    f.write('\n')

在线尝试

英文:

You will need to chunk all strings, and then iterate over the chunks simultaneously using zip. Now, block is a tuple containing the individual chunks of each string

chunks = [chunkstring(s, 60) for s in (str1, str2, str3)]

for block in zip(*chunks):
    for c in block:
        # write all lines in block
        f.write(c)
        f.write('\n')
    # at the end of the block, write a blank line 
    f.write('\n')

Try it online

答案2

得分: 1

你可以进一步采用 Pranav 的 zip 方法,甚至将其用于分块:

s1 = "a" * 180
s2 = "b" * 180
s3 = "c" * 180

def chunk60(s): return map("".join, zip(*[iter(s)] * 60))
for lines in zip(*map(chunk60, (s1, s2, s3))):
    print(*lines, sep='\n')
    print("...")
    
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
...
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
...
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
...

注意:我已将 HTML 实体引号转换为普通引号以便于阅读。

英文:

You could push Pranav's zip approach even further and use zip for the chunking as well:

s1 = "a"*180
s2 = "b"*180
s3 = "c"*180

def chunk60(s): return map("".join,zip(*[iter(s)]*60))
for lines in zip(*map(chunk60,(s1,s2,s3))):
    print(*lines,sep='\n')
    print("...")

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
...
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
...
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
...

huangapple
  • 本文由 发表于 2023年2月13日 23:43:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定