0

私はPythonを初めて使用します。今日は、リストに基づいてPython3.3で簡単なテストプログラムを作成していました。そのため、タブスペース文字を入力しているときに\t、出力が点滅して改行文字を入力していることに気付きました。サンプルを以下に示します。

 def printlist(list_name,tabs=0):
         for items in list_name:
           if isinstance(items,list):
             printlist(items,tabs)
           else:
             print(items)
             for num in range(tabs):
                 print('\t') #tab-stops provide

    list3 = [
             'list no. 3',
             ['tree','stems','root'],
             ['human','hand','leg'],
             ['robot','microprocessor','motor']
            ]

   printlist(list3,1)

そして、出力は次のとおりです。

    >>> ================================ RESTART ================================
>>> 
list no. 3


tree


stems


root


human


hand


leg


robot


microprocessor


motor


>>>

しかし、私が意図した出力形式は次のとおりです。

    list no. 3
    tree
    stems
    root
    human
    hand
    leg
    robot
    microprocessor
    motor

[改行ではなくタブが欲しい]

では、どうすればそれが可能になるのでしょうか。

4

2 に答える 2

5

デフォルトでprint()は、改行で終了します。この動作を抑制したい場合は、を指定してendください。

print("\t", end="")

ドキュメントはこちらです。 http://docs.python.org/3.3/library/functions.html#print

于 2013-01-06T16:28:08.887 に答える
1

print のデフォルトの動作は、改行を追加することです。たとえば、http ://docs.python.org/3/whatsnew/3.0.html を参照してください。

于 2013-01-06T16:33:55.523 に答える