在同一行打印多行字符串(ascii 艺术)的最好和最短的方法,即使在 Python 中高度不同[重复]

问题描述 投票:0回答:2
# Rock
rock = ("""Rock
    _______
---'   ____)
      (_____)
      (_____)   
      (____)
---.__(___)
""")

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

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

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

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

谢谢

python ascii multiline
2个回答
0
投票

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

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                
     _______            _______              _______            _______         
---'    ____)____   ---'   ____)____    ---'    ____)____   ---'   ____)        
           ______)            ______)              ______)        (_____)       
          _______)         __________)            _______)        (_____)       
         _______)         (____)                 _______)         (____)        
         _______)   ---.__(___)                  _______)   ---.__(___)         
         _______)                                _______)                       
---.__________)                         ---.__________)                         

0
投票
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
等是包含几个换行符的single字符串,我们想在同一行上打印
rock
的一行,
paper
的一行和
scissors
的一行,所以我们需要做的第一件事是拆分每个字符串以获得字符串列表

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)

此时我们可以使用适当的格式打印

transposed_list
中的每个三元组元素(
%-25s
输出一个25个字符长的字符串,右边空白填充)。

使这个功能留作练习……


副—解说


IMO,一个函数应该尽可能不提供副作用,比如打印。

© www.soinside.com 2019 - 2024. All rights reserved.