将字符串从水平转换为垂直

2024-10-01 17:32:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道转动以下字符串的过程:

Hi
Hello
Goodbye

看起来像这样:

    e
    y
  o b
  l d
  l o
i e o
H H G

因此,水平线将转换为垂直线,但从楼板开始,而不是从天花板开始

如果有人能提供一些建议,帮助我获得正确的功能或命令,那么我将不胜感激。我还想知道如何处理长度为x的字符串列表,其中x是大于零的任何数字。我已经用for x in list():函数进行了实验,但是似乎无法产生上述输出

提前谢谢


Tags: 字符串命令功能hello列表for过程数字
2条回答

你可以用熊猫、Python3来完成。它可能不适合于生产代码库,但它可以工作

import pandas as pd

input_value = """Hi
Hello
Goodbye
"""

list_of_chars = list(map(lambda x: [*x], input_value.split("\n")))

df = pd.DataFrame(list_of_chars)
df = df.T[::-1].fillna(value=' ')

for index, row in df.iterrows():
    print(" ".join(row.values))

Output

唤醒大脑的有趣挑战。请尝试以下代码:

def vertical_print_generator(data, max_length):
    i = max_length
    while i > 0:
        i -= 1
        yield [s[i] for s in data]


if __name__ == '__main__':
    in_data = ('Hi', 'Hello', 'Goodbye')
    max_length = max(map(len, in_data))
    out_data = ['{:{max_length}}'.format(s, max_length=max_length) for s in in_data]
    for row in vertical_print_generator(out_data, max_length):
        print(' '.join(row))

相关问题 更多 >

    热门问题