python:水平打印而不是当前默认打印

2024-05-13 03:58:15 发布

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

我想知道我们是否可以像python中的row-wise一样打印。

基本上,我有一个循环,可能会持续百万次,我在这个循环中打印出一些战略要点。。所以如果我能像行一样打印就太酷了

print x
# currently gives
# 3
# 4
#.. and so on

我看起来像

print x
# 3 4

Tags: andsoonrowprint要点战略wise
3条回答

只需在打印的项目末尾添加一个,

print x,
# 3 4

可以在调用print后添加逗号以避免换行:

print 3,
print 4,
# produces 3 4

在Python2中:

data = [3, 4]
for x in data:
    print x,    # notice the comma at the end of the line

或者在Python3中:

for x in data:
    print(x, end=' ')

印刷品

3 4

相关问题 更多 >