简体   繁体   English

Python:将字符串数组逐行打印到stdout

[英]Python: print array of strings to stdout line by line

I need to print to stdout (but next step is print to file) an array of strings, containing the info to build-up a shape. 我需要打印到标准输出(但下一步是打印到文件)一个字符串数组,其中包含建立形状的信息。 Below, the for loop: 下面是for循环:

for i in range(0, xMax):
    for j in range(0, yMax):
        print arena_shape[ j + i*yMax]
    print('\r')

The array arena_shape[] contains the chars to built-up a 2D figure, figure with i = [0, xMax] rows and j = [0, yMax] columns. 数组arena_shape []包含用于构建2D图形的字符,该图形具有i = [0,xMax]行和j = [0,yMax]列。 I expect the output to be : 我希望输出为:

i=0    ----------    ------
i=1    |                  |
       |                  |
       |                  |
       |                  |
       |                  |
       |                  |
i=xMax --------------------

instead to get the first line (i=0, j=[0,yMax]) in "horizontal" as I would expect, I get it displayed in vertical, even if I tell python to \\r only when changing row i and not at each column j 而是像我期望的那样,在“水平”中获得第一行(i=0, j=[0,yMax]) ,我将它显示为垂直,即使我仅在更改row i而不是告诉python到\\r时也是如此在每column j

i=0 -
    -
    -
    -
    -
    -
    -
    -
    -
    -




    -
    -
    -
    -
    -
    -

I don't understand why this is not changing line after the print("\\r") instruction. 我不明白为什么在print("\\r")指令后这行没有改变。 Thank you in advance for the help. 预先感谢您的帮助。

The print function in Python always prints in a new line. Python中的print功能始终在新行中打印。 If you want to print without a new line i suggest storing what you need to print in a temporary variable and printing it only when you exit the inner for loop 如果您想在没有换行的情况下进行打印,建议您将需要打印的内容存储在一个临时变量中,并仅在退出内部for循环时才进行打印

for i in range(0, xMax):
    temp_variable = ''
    for j in range(0, yMax):
        temp_variable += arena_shape[ j + i*yMax]
    print temp_variable

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

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