Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何阻止Python在迭代时添加空格?_Python_Arrays_Multidimensional Array_Iteration - Fatal编程技术网

如何阻止Python在迭代时添加空格?

如何阻止Python在迭代时添加空格?,python,arrays,multidimensional-array,iteration,Python,Arrays,Multidimensional Array,Iteration,我的Python代码: mapArray = [["#","#","#"],["#","#","#"],["#","#","#"]] for row in mapArray: for cell in row: print cell, print print 打印此文件: # # # # # # # # # 为什么不这样做: ### ### ### 多谢 将您的打印单元格,更改为sys.stdout.write(单元格)。当然,在导入sys之后。将打印

我的Python代码:

mapArray = [["#","#","#"],["#","#","#"],["#","#","#"]]
for row in mapArray:
    for cell in row:
            print cell,
    print
print
打印此文件:

# # #
# # #
# # #
为什么不这样做:

###
###
###

多谢

将您的
打印单元格,
更改为
sys.stdout.write(单元格)
。当然,在导入
sys
之后。

打印单元格更改为
sys.stdout.write(单元格)
。当然,在导入
sys
之后。

当我希望Python只打印我告诉它的内容而不插入换行符或空格时,我的首选解决方案是使用
sys.stdout

from sys import stdout
mapArray = [["#","#","#"],["#","#","#"],["#","#","#"]]
for row in mapArray:
    for cell in row:
            stdout.write(cell)
    stdout.write("\n")
stdout.write("\n")

状态为,
“在每个对象(转换和)写入之前写入一个空格,除非输出系统认为它位于行的开头。”
这就是为什么
sys.stdout
是这里的首选解决方案,也是您在输出中看到空格的原因。

当我希望Python只打印我告诉它的内容而不插入换行符或空格时,我的首选解决方案是使用
sys.stdout

from sys import stdout
mapArray = [["#","#","#"],["#","#","#"],["#","#","#"]]
for row in mapArray:
    for cell in row:
            stdout.write(cell)
    stdout.write("\n")
stdout.write("\n")

状态为,
“在每个对象(转换和)写入之前写入一个空格,除非输出系统认为它位于行的开头。”
这就是为什么
sys.stdout
是这里的首选解决方案,也是您在输出中看到空格的原因。

或者您可以简单地使用
join
构建字符串,然后打印它

>>> mapArray = [["#","#","#"],["#","#","#"],["#","#","#"]]
>>> print '\n'.join([''.join(line) for line in mapArray])
###
###
###

或者您可以简单地使用
join
构建字符串,然后打印它

>>> mapArray = [["#","#","#"],["#","#","#"],["#","#","#"]]
>>> print '\n'.join([''.join(line) for line in mapArray])
###
###
###

仅供参考,python文档有一些关于何时决定添加这样的空间的信息。参见.FYI,python文档提供了一些关于何时决定添加这样的空间的信息。请参阅。注意,print将其给定的对象转换为字符串,而file-like对象的
write
方法则使用字符串。您可能需要
sys.stdout.write(str(cell))
,这取决于
cell
是什么。@Darin:如果您不想切换到Python3,它的打印函数的两个关键字参数是“sep”和“end”(因为打印不再是一个语句,而是Python3中的一个函数),它最吸引人。默认情况下,sep被指定为空格字符,end被指定为\n,因此print('a','b')将返回“a b”,后跟一个换行符,但print('a','b',sep='',end='')将返回“ab”,且没有尾随的换行符。print()(无参数)将换行发送到输出,就像在Python 2中使用的print一样。@JAB:Python 2.6已经有了
print
函数(如果需要):
from\uuuuuu future\uuuuu导入print\u函数;印刷品(“a”、“b”、sep=“”、end=“”);打印(“c”)
Philipp:哦,是的,我忘了。我知道2.6在某种程度上是向前兼容的,但我忘记了它的细节。请注意,print将其给定的对象转换为字符串,而file-like-objects的
write
方法接受字符串。您可能需要
sys.stdout.write(str(cell))
,这取决于
cell
是什么。@Darin:如果您不想切换到Python3,它的打印函数的两个关键字参数是“sep”和“end”(因为打印不再是一个语句,而是Python3中的一个函数),它最吸引人。默认情况下,sep被指定为空格字符,end被指定为\n,因此print('a','b')将返回“a b”,后跟一个换行符,但print('a','b',sep='',end='')将返回“ab”,且没有尾随的换行符。print()(无参数)将换行发送到输出,就像在Python 2中使用的print一样。@JAB:Python 2.6已经有了
print
函数(如果需要):
from\uuuuuu future\uuuuu导入print\u函数;印刷品(“a”、“b”、sep=“”、end=“”);打印(“c”)
Philipp:哦,是的,我忘了。我知道2.6在某种程度上是向前兼容的,但我忘记了它的具体内容。谢谢你的文档引用。很高兴知道为什么,而不仅仅是如何。感谢文档引用。很高兴知道为什么,而不仅仅是如何。