【问题标题】:python: printing horizontally rather than current default printingpython:水平打印而不是当前默认打印
【发布时间】:2011-12-08 21:31:05
【问题描述】:

我想知道我们是否可以像在 python 中那样按行打印。

基本上我有一个循环,它可能会循环数百万次,我正在打印该循环中的一些战略计数。所以如果我可以像行方式那样打印,那就太酷了

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

我看起来像

print x
# 3 4

【问题讨论】:

  • 你总是可以连接 x 并在循环完成后打印:)

标签: python


【解决方案1】:

只需在要打印的项目末尾添加,。

print(x,)
# 3 4

或者在 Python 2 中:

print x,
# 3 4

【讨论】:

  • 它不起作用。这给我SyntaxError: Missing parentheses in call to 'print'. Did you mean print(1, end=" ")?
  • 这个例子,从 2011 年开始,只适用于 Python2。 :)
  • print(x,) 用于当前版本。
【解决方案2】:

在 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

【讨论】:

    【解决方案3】:

    您不需要使用 for 循环来执行此操作!

    mylist = list('abcdefg')
    print(*mylist, sep=' ') 
    # Output: 
    # a b c d e f g
    

    这里我使用解包操作符作为迭代器:*。在后台打印函数被调用如下:print('a', 'b', 'c', 'd', 'e', 'f', 'g', sep=' ')。

    此外,如果您更改sep 参数中的值,您可以自定义您想要的打印方式,例如:

    print(*mylist, sep='\n') 
    # Output: 
    # a
    # b
    # c
    # d
    # e
    # f
    # g
    

    【讨论】:

      【解决方案4】:

      使用此代码进行打印

      print(x,end="")

      【讨论】:

        【解决方案5】:

        如果您在末尾添加逗号,它应该适合您。

        >>> def test():
        ...    print 1,
        ...    print 2,
        ... 
        >>> test()
        1 2
        

        【讨论】:

        • 它不起作用。这给我SyntaxError: Missing parentheses in call to 'print'. Did you mean print(1, end=" ")?
        【解决方案6】:
        my_list = ['keyboard', 'mouse', 'led', 'monitor', 'headphones', 'dvd']
        for i in xrange(0, len(my_list), 4):
            print '\t'.join(my_list[i:i+4])
        

        【讨论】:

          【解决方案7】:
          a=int(input("RangeFinal "))
          print("Prime Numbers in the range")
          for n in range(2, a):
             p=0
             for x in range(2, n):
                  if n % x == 0:
                          break
                  else:
                     if(p==0):
                        print(n,end=' ')
                        p=1
          

          回答

          RangeFinal 19
          Prime Numbers in the range
          3 5 7 9 11 13 15 17 
          

          【讨论】:

            【解决方案8】:

            对于python 2:

            for x in num:
                print x,
            

            对于python 3:

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

            【讨论】:

              【解决方案9】:

              Python 3:

              l = [3.14, 'string', ('tuple', 'of', 'items')]
              print(', '.join(map(repr, l)))
              

              输出:

              3.14, 'string', ('tuple', 'of', 'items')
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2011-09-15
                • 2021-05-16
                • 1970-01-01
                • 1970-01-01
                • 2012-09-19
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多