在同一行上打印多行字符串(ASCII ART)的最佳和最短方法,即使高度不同

发布于 2025-02-05 03:54:55 字数 487 浏览 1 评论 0原文

# Rock
rock = ("""Rock
    _______
---'   ____)
      (_____)
      (_____)   
      (____)
---.__(___)
""")

# Paper
paper = ("""Paper
     _______
---'    ____)____
           ______)
          _______)  
         _______)
---.__________)
""")

# Scissors
scissors = ("""Scissors
    _______
---'   ____)____
          ______)
       __________)  
      (____)
---.__(___)
""")

如何在同一行上打印这些多行字符串?

我正在寻找最简单,最短的方法 我尝试了一些我看到的技巧,但到目前为止没有运气。

谢谢

# Rock
rock = ("""Rock
    _______
---'   ____)
      (_____)
      (_____)   
      (____)
---.__(___)
""")

# Paper
paper = ("""Paper
     _______
---'    ____)____
           ______)
          _______)  
         _______)
---.__________)
""")

# Scissors
scissors = ("""Scissors
    _______
---'   ____)____
          ______)
       __________)  
      (____)
---.__(___)
""")

how can I print these multiline strings at the same line?

I'm looking for the simplest and shortest way
I tried few tricks that I saw, but no luck so far.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下雨或天晴 2025-02-12 03:54:55

一条线上的任意数量图像(纸张有额外的手指来证明它仍然适用于不同的图像尺寸):

import itertools

def print_on_line(*images):
    lines = list(map(str.splitlines, images))
    max_length = max(map(len, itertools.chain.from_iterable(lines)))
    lines_padded = [[line.ljust(max_length, " ") for line in image]
                    for image in lines]
    lines_combined = itertools.zip_longest(*lines_padded, 
                                           fillvalue=" " * max_length)
    print("\n".join(map("".join, lines_combined)))


print_on_line(paper, scissors, paper, rock)


# out:
Paper               Scissors            Paper               Rock                
     _______            _______              _______            _______         
---'    ____)____   ---'   ____)____    ---'    ____)____   ---'   ____)        
           ______)            ______)              ______)        (_____)       
          _______)         __________)            _______)        (_____)       
         _______)         (____)                 _______)         (____)        
         _______)   ---.__(___)                  _______)   ---.__(___)         
         _______)                                _______)                       
---.__________)                         ---.__________)                         

Arbitrary number of images on one line (paper got an extra finger to demonstrate that it still works for different image sizes):

import itertools

def print_on_line(*images):
    lines = list(map(str.splitlines, images))
    max_length = max(map(len, itertools.chain.from_iterable(lines)))
    lines_padded = [[line.ljust(max_length, " ") for line in image]
                    for image in lines]
    lines_combined = itertools.zip_longest(*lines_padded, 
                                           fillvalue=" " * max_length)
    print("\n".join(map("".join, lines_combined)))


print_on_line(paper, scissors, paper, rock)


# out:
Paper               Scissors            Paper               Rock                
     _______            _______              _______            _______         
---'    ____)____   ---'   ____)____    ---'    ____)____   ---'   ____)        
           ______)            ______)              ______)        (_____)       
          _______)         __________)            _______)        (_____)       
         _______)         (____)                 _______)         (____)        
         _______)   ---.__(___)                  _______)   ---.__(___)         
         _______)                                _______)                       
---.__________)                         ---.__________)                         
旧话新听 2025-02-12 03:54:55
In [19]: for rps in zip(*(thing.split('\n') for thing in (rock, paper, scissors))):
    ...:     print(("%-25s"*3)%(rps))
    ...: 
Rock                     Paper                    Scissors                 
    _______                   _______                 _______              
---'   ____)             ---'    ____)____        ---'   ____)____         
      (_____)                       ______)                 ______)        
      (_____)                      _______)              __________)       
      (____)                      _______)              (____)             
---.__(___)              ---.__________)          ---.__(___)              
                                                                           

In [20]: 

更灵活?

In [24]: def pol(*list_of_things):
    ...:     return '\n'.join(("%-25s"*len(list_of_things))%(rps) for rps in zip(*(thing.split('\n') for thing in list_of_things)))

In [25]: print(pol(scissors, scissors, rock))
Scissors                 Scissors                 Rock                     
    _______                  _______                  _______              
---'   ____)____         ---'   ____)____         ---'   ____)             
          ______)                  ______)              (_____)            
       __________)              __________)             (_____)            
      (____)                   (____)                   (____)             
---.__(___)              ---.__(___)              ---.__(___)              
                                                                           

In [26]: 

评论


rock等是包含一些newline字符的字符串,我们想在同一条线上打印一条来自Rock ,paper的行,剪刀的一行,因此我们需要做的第一件事是拆分每个字符串以获取列表的列表字符串,

list_of_lists_of_strings = [thing.split('\n') for thing in (rock, paper, scissors)]
# [['rock', '    _______', ...], ['paper', '    _______', ...], [...]]

但我们确实需要

[['rock', 'paper', 'scissors'],
 ['    _______', '    _______', '    _______'],
 [..., ..., ...]
 ...,
]

,也就是字符串列表的转换,但这是python中的一个众所周知的成语(这不是真正的列表,但是…),

transposed_list = zip(*list_of_lists_of_strings)

这是我们可以在此时打印的每个元素的每个三重元素transpoded_list使用适当的格式(%-25S输出字符串长25个字符,右侧为空白)。

使该功能作为练习……


sub -commentary


imo,尽可能地不应提供副作用,例如打印。

In [19]: for rps in zip(*(thing.split('\n') for thing in (rock, paper, scissors))):
    ...:     print(("%-25s"*3)%(rps))
    ...: 
Rock                     Paper                    Scissors                 
    _______                   _______                 _______              
---'   ____)             ---'    ____)____        ---'   ____)____         
      (_____)                       ______)                 ______)        
      (_____)                      _______)              __________)       
      (____)                      _______)              (____)             
---.__(___)              ---.__________)          ---.__(___)              
                                                                           

In [20]: 

More flexibility?

In [24]: def pol(*list_of_things):
    ...:     return '\n'.join(("%-25s"*len(list_of_things))%(rps) for rps in zip(*(thing.split('\n') for thing in list_of_things)))

In [25]: print(pol(scissors, scissors, rock))
Scissors                 Scissors                 Rock                     
    _______                  _______                  _______              
---'   ____)____         ---'   ____)____         ---'   ____)             
          ______)                  ______)              (_____)            
       __________)              __________)             (_____)            
      (____)                   (____)                   (____)             
---.__(___)              ---.__(___)              ---.__(___)              
                                                                           

In [26]: 

Commentary


rock, etc are single strings containing a few newline characters, and we want to print, on the same line, a line from rock, a line from paper and one from scissors, so the first thing that we need to do is to split each string to obtain a list of list of strings

list_of_lists_of_strings = [thing.split('\n') for thing in (rock, paper, scissors)]
# [['rock', '    _______', ...], ['paper', '    _______', ...], [...]]

but we really need

[['rock', 'paper', 'scissors'],
 ['    _______', '    _______', '    _______'],
 [..., ..., ...]
 ...,
]

that is, the transpose of the list of lists of strings, but this is a well known idiom in Python (it's not really a list, but…)

transposed_list = zip(*list_of_lists_of_strings)

at this point we can print each triple of elements in the transposed_list using an appropriate format (%-25s outputs a string 25 characters long, blank filled on the right).

Making this a function is left as an exercise…


Sub—commentary


IMO, as far as possible a function shouldn't provide side effects, like printing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文